0

这就是我所拥有的,但孩子的数量从未打印出来。我得到原始 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;
        }
    }    
4

2 回答 2

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");
于 2014-03-03T20:59:53.430 回答
0

你没有进入数据对象......你的列表项中只有“种类”和“数据”标签,所以首先进入“数据”标签然后得到“孩子”。试试这样:

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;
}
于 2014-03-03T20:58:13.183 回答