2016-12-27 84 views
0

我試圖從reddit(https://www.reddit.com/r/gifs/.json)從json文件中獲取數據。但我不斷收到同樣的錯誤:org.json.JSONException:沒有值的兒童

org.json.JSONException: No value for children

這是我的代碼:

RequestQueue queue = Volley.newRequestQueue(this); 
String url ="https://www.reddit.com/r/funny.json"; 

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
    @Override 
    public void onResponse(JSONObject response) { 
    try { 
     JSONArray children = response.getJSONArray("children"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     Log.i(TAG, "ERROR !!!!" + e); 
    } 
    } 
}, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
     Log.i(TAG, "ERROR"); 
     } 
}); 

queue.add(jsObjRequest); 

如果有人能幫助我就好!

+1

權的孩子,因爲'children' 'JSONArray'在數據''JSONObject'內部 –

回答

0

你應該做這樣的childrendata對象

JsonObject data = response.getJsonObject("data"); 
JsonArray children = data.getJsonArray("children"); 
0

完成這個任務,你的孩子陣列裏面的數據對象

JSONObject data = response.getJSONObject("data"); 
    JSONArray children = data.getJSONArray("children");