简体   繁体   English

如何使用Java访问此JSON数组?

[英]How do I access this JSON Array in Java?

This is what I have, but the number of children never prints out. 这就是我所拥有的,但是孩子的数量从来没有打印出来。 I'm getting the raw JSON, then making a JSONArray, accessing the second member's children. 我正在获取原始JSON,然后制作一个JSONArray,访问第二个成员的孩子。 What am I missing here? 我在这里想念什么? I have similar code that works perfectly, only difference is in the JSON, it does not start with an array 我有类似的代码可以完美地工作,只是区别在于JSON,它不是以数组开头

JSON Input: 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
          }
       }
    ]

My code: 我的代码:

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;
        }
    }    

You didn't get into data object... You only have "kind" and "data" tags in your list items, so first get into "data" tag then get "children". 您没有进入数据对象……列表项中只有“ kind”和“ data”标签,因此首先进入“ data”标签,然后获得“ children”。 Try like this: 尝试这样:

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;
}

Your JSON array contains objects that have a field "data" that contains an object that contains a field "children". 您的JSON数组包含具有字段“ data”的对象,该字段包含一个包含字段“ children”的对象。

You're doing: 你在做:

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

You missed the data field. 您错过了data字段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM