2014-03-03 4 views
0

이것은 내가 가지고있는 것이지만, 아이들의 수가 결코 인쇄되지 않습니다. 원시 JSON을 가져온 다음 JSONArray를 만들어 두 번째 멤버의 자식에 액세스합니다. 내가 여기서 무엇을 놓치고 있니?Java에서이 JSON Array에 어떻게 액세스합니까?

[ 
    { 
     "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; 
     } 
    }  
+0

실제 질문에 대해 더 자세히 설명해 주시겠습니까? 어떤 수업/라이브러리를 사용해야하는지 묻고 있습니까? 아니면 엄격한 JSON 관련 질문입니까? – Sebastiano

+0

여기서 정말로 필요한 것은 JSONArray 내부의 데이터 유형을 식별하는 것입니다. 다른 JSON 객체 인 경우에는 그 객체를 구문 분석해야합니다. 그렇지 않으면 항목 또는 json 객체 목록으로 구문 분석해야합니다. – Eenvincible

답변

0

내가 완벽하게 작동 유사한 코드를 가지고, 유일한 차이점은 JSON에서, 그것은 배열을

JSON 입력을 시작하지 않습니다 데이터 객체로 가져 오기 ... 목록 항목에 "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; 
} 
0

귀하의 JSON 배열이 "아이들"의 필드를 포함하는 객체를 포함하는 필드 "데이터"가 개체를 포함합니다.

당신은 일을하는지 :

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"); 
관련 문제