0

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. 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 Input:

[
   {
      "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;
        }
    }    
sanghas26
  • 161
  • 1
  • 2
  • 10
  • Could you be more specific about the actual question? Are you asking which classes/libraries you should be using? Or is it a strict JSON-related question? – Sebastiano Mar 03 '14 at 20:51
  • What you really need here is to identify the TYPE of data inside the JSONArray. If if it is another JSON object, then you have to parse it as such, otherwise parse it as either an item or a list of json objects. – Eenvincible Mar 03 '14 at 20:52

2 Answers2

0

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". 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;
}
yahya
  • 4,810
  • 3
  • 41
  • 58
0

Your JSON array contains objects that have a field "data" that contains an object that contains a field "children".

You're doing:

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

You missed the data field.

JSONObject obj = new JSONArray(raw).getJSONObject(1);
JSONArray children = obj.getJSONObject("data").getJSONArray("children");
Brian Roach
  • 76,169
  • 12
  • 136
  • 161