如何在Java中访问此JSON数组?

时间:2014-03-03 20:49:05

标签: java android json android-json

这就是我所拥有的,但孩子的数量永远不会打印出来。我正在获取原始JSON,然后创建一个JSONArray,访问第二个成员的子节点。我在这里错过了什么?我有类似的代码可以完美地工作,只有区别在于JSON,它不是以数组

开头

JSON输入:

[
   {
      "kind":"Listing",
      "data":{
         "modhash":"",
         "children":[
            {
               "kind":"t3",
               "data":{
                  "domain":"",
                  "banned_by":null,
                  "media_embed":{    
                      },
                      "subreddit":"",
                      "selftext_html":"",
                      "selftext":"",
                      "likes":null,
                      "secure_media":null,
                      "link_flair_text":null,
                      "id":"1zeek5",
                      "secure_media_embed":{},
                      "clicked":false,
                      "stickied":false,
                      "author":"xVictoryy",
                      "media":null,
                      "score":1,
                      "approved_by":null,
                      "over_18":false,
                      "hidden":false,
                      "thumbnail":"",
                      "subreddit_id":"t5_2sdpm",
                      "edited":false,
                      "link_flair_css_class":null,
                      "author_flair_css_class":null,
                      "downs":0,
                      "saved":false,
                      "is_self":true,
                      "permalink":"",
                      "name":"t3_1zeek5",
                      "created":1393843471.0,
                      "url":"",
                      "author_flair_text":null,
                      "title":"Seeking advice.",
                      "created_utc":1393814671.0,
                      "ups":1,
                      "num_comments":3,
                      "visited":false,
                      "num_reports":null,
                      "distinguished":null
                   }
                }
             ],
             "after":null,
             "before":null
          }
       },
       {
          "kind":"Listing",
          "data":{
             "modhash":"",
             "children":[
                {
                   "kind":"t1",
                   "data":{
                      "subreddit_id":"t5_2sdpm",
                      "banned_by":null,
                      "subreddit":"",
                      "likes":null,
                      "replies":{
                         "kind":"Listing",
                         "data":{
                            "modhash":"",
                            "children":[
                               {
                                  "kind":"t1",
                                  "data":{
                                     "subreddit_id":"t5_2sdpm",
                                     "banned_by":null,
                                     "subreddit":"cscareerquestions",
                                     "likes":null,
                                     "replies":"",
                                     "saved":false,
                                     "id":"cfsxjqn",
                                     "gilded":0,
                                     "author":"xVictoryy",
                                     "parent_id":"t1_cfsx26m",
                                     "approved_by":null,
                                     "body":"",
                                     "edited":false,
                                     "author_flair_css_class":null,
                                     "downs":0,
                                     "body_html":"",
                                     "link_id":"t3_1zeek5",
                                     "score_hidden":false,
                                     "name":"t1_cfsxjqn",
                                     "created":1393845230.0,
                                     "author_flair_text":null,
                                     "created_utc":1393816430.0,
                                     "distinguished":null,
                                     "num_reports":null,
                                     "ups":1
                                  }
                               }
                            ],
                            "after":null,
                            "before":null
                         }
                      },
                      "saved":false,
                      "id":"cfsx26m",
                      "gilded":0,
                      "author":"dauphic",
                      "parent_id":"t3_1zeek5",
                      "approved_by":null,
                      "body":"A lot of schools don't expect high school Calculus.",
                      "edited":false,
                      "author_flair_css_class":"",
                      "downs":0,
                      "body_html":"",
                      "link_id":"t3_1zeek5",
                      "score_hidden":false,
                      "name":"t1_cfsx26m",
                      "created":1393844079.0,
                      "author_flair_text":"Software Engineer",
                      "created_utc":1393815279.0,
                      "distinguished":null,
                      "num_reports":null,
                      "ups":1
                   }
                },
                {
                   "kind":"t1",
                   "data":{
                      "subreddit_id":"t5_2sdpm",
                      "banned_by":null,
                      "subreddit":"cscareerquestions",
                      "likes":null,
                      "replies":"",
                      "saved":false,
                      "id":"cft3lbj",
                      "gilded":0,
                      "author":"I_EAT_GUSHERS",
                      "parent_id":"t3_1zeek5",
                      "approved_by":null,
                      "body":"",
                      "edited":false,
                      "author_flair_css_class":"",
                      "downs":0,
                      "body_html":"",
                      "link_id":"t3_1zeek5",
                      "score_hidden":false,
                      "name":"t1_cft3lbj",
                      "created":1393864015.0,
                      "author_flair_text":"Looking for internship",
                      "created_utc":1393835215.0,
                      "distinguished":null,
                      "num_reports":null,
                      "ups":1
                   }
                }
             ],
             "after":null,
             "before":null
          }
       }
    ]

我的代码:

List<Comment> fetchComments() {
        Log.d("running", "attempting fetch...");
        String raw = RemoteData.readContents(url);
        List<Comment> list = new ArrayList<Comment>();
        try {
            JSONObject data = new JSONArray(raw).getJSONObject(1);
            JSONArray children = data.getJSONArray("children");
            Log.d("running", "comments: " + children.length());
            }
        } catch (Exception e) {
            Log.e("fetchComments()", e.toString());
        }
        return list;
    }

public static String readContents(String url){        
        HttpURLConnection hcon=getConnection(url);
        if(hcon==null) return null;
        try{
            StringBuffer sb=new StringBuffer(8192);
            String tmp="";
            BufferedReader br=new BufferedReader(
                                new InputStreamReader(
                                        hcon.getInputStream()
                                )
                              );
            while((tmp=br.readLine())!=null)
                sb.append(tmp).append("\n");
            br.close();                        
            return sb.toString();
        }catch(IOException e){
            Log.d("READ FAILED", e.toString());
            return null;
        }
    }    

2 个答案:

答案 0 :(得分:0)

您没有进入数据对象...您的列表项中只有“kind”和“data”标签,因此首先进入“data”标签然后获取“children”。试试这样:

List<Comment> fetchComments() {
    Log.d("running", "attempting fetch...");
    String raw = RemoteData.readContents(url);
    List<Comment> list = new ArrayList<Comment>();
    try {
        JSONObject data = new JSONArray(raw).getJSONObject(1);
        JSONArray children = data.getJSONObject("data").getJSONArray("children");
        Log.d("running", "comments: " + children.length());
        }
    } catch (Exception e) {
        Log.e("fetchComments()", e.toString());
    }
    return list;
}

答案 1 :(得分:0)

您的JSON数组包含具有字段“data”的对象,该字段包含一个包含字段“children”的对象。

你在做:

JSONObject data = new JSONArray(raw).getJSONObject(1);
JSONArray children = data.getJSONArray("children");

您错过了data字段。

JSONObject obj = new JSONArray(raw).getJSONObject(1);
JSONArray children = obj.getJSONObject("data").getJSONArray("children");