2016-09-08 2 views
-2

上位オブジェクトを呼び出す必要なくエントリ配列にアクセスできますか?どのように私はエントリの配列にアクセスできますか私に例を提供してください。 JSONデータの抽出項目や抽出方法が異なると、このことが非常に混乱します。私はクエリをうまく理解していないと思います。私は配列[]とオブジェクト{}の違いを理解していますが、まだ構図について学んでいます。おかげさまで オブジェクトに配列を呼び出す。 (Android、JSON)

JSON:

{ 
    "responseData":{ 
     "feed":{ 
     "feedUrl":"", 
     "title":"", 
     "link":"", 
     "author":"", 
     "description":"", 
     "type":"", 
     "entries":[ 
      { 
       "title":"", 
       "link":"", 
       "author":"", 
       "publishedDate":"", 
       "contentSnippet":"", 
       "content":"", 
       "categories":[ 
        "" 
       ] 
      }, 
      { 
       "title":"", 
       "link":"", 
       "author":"", 
       "description":"", 
       "type":"", 
       "entries":[ 
        { 
        "title":"", 
        "link":"", 
        "author":"", 
        "publishedDate":"", 
        "contentSnippet":"", 
        "content":"", 
        "categories":[ 
         "" 
        ] 
        } 
       ] 
      } 
     }, 
     "responseDetails":null, 
     "responseStatus":200 
     } 

コード:あなたは、上のオブジェクトを呼び出すことなく、エントリの配列を取得することはできません

HttpEntity entity = response.getEntity(); 
String data = EntityUtils.toString(entity); 
JSONObject jsono = new JSONObject(data); 
JSONArray jarray = jsono.getJSONArray("entries"); 
NewsList.content = (int)jarray.length(); 
for (int i = 0; i < jarray.length(); i++) { 
    JSONObject object = jarray.getJSONObject(i); 
    NewsList News = new NewsList(); 
    News.setId(object.getString("title")); 
    News.setName(object.getString("publishedDate")); 
    News.setAuthor(object.getString("link")); 
    NewsListList.add(News); 
} 
+0

ここに入力したJsonは正しい形式ではありません。正しいもので親切に更新してください。 – dhuma1981

+0

ええ、GSONを使って簡単にこれを行うことができます。 –

+0

あなたのjsonは間違っています – Nikhil

答えて

0

号。

このようなjsonレスポンスを解析できます。

JSONObject jsono = new JSONObject(data); 
JSONObject json1 = jsono.getJSONObject("responseData"); 
JSONObject json2 = json1.getJSONObject("feed"); 
String feedUrl = json2.getString("feedUrl"); 
JSONArray jarray = json2.getJSONArray("entries"); 
NewsList.content = (int)jarray.length(); 
for (int i = 0; i < jarray.length(); i++) { 
JSONObject object = jarray.getJSONObject(i); 
NewsList News = new NewsList(); 
News.setId(object.getString("title")); 
News.setName(object.getString("publishedDate")); 
News.setAuthor(object.getString("link")); 
NewsListList.add(News); 
} 

希望すると、これが役立ちます。

+0

乾杯。よく説明され、まっすぐです。 – jaroman777

関連する問題