簡體   English   中英

如何使用Java訪問此JSON數組?

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

這就是我所擁有的,但是孩子的數量從來沒有打印出來。 我正在獲取原始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;
        }
    }    

您沒有進入數據對象……列表項中只有“ 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;
}

您的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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM