2016-12-06 8 views
0

アンドロイドで次のようなJSONデータを解析しようとしています。これまでのところ私が解析するために管理しているAndroidでネストされたJSONデータを解析する

をクリックし、そうで....私は私がリストビューのリスト項目を経由して、それを選択すると、特定のノードのデータを参照することが出来るのです

{ 
"data": [{ 
    "http_code": 200, 
    "message": "success", 
    "created_at": "2016/10/12", 
     "categories": [{ 
      "cat_id": 1, 
      "cat_name": "Business and Commerce", 
       "subcategories":[{ 
        "subcat_id": 1, 
        "subcat_name": "Intellectual Property", 
         "articles":[{ 
          "article_id": 1, 
          "article_heading": "What is intellectual?", 
          "article_url": "http://example.com/1" 
         },{ 
          "article_id": 2, 
          "article_heading": "Types of intellectual Properties", 
          "article_url": "http://example.com/2" 
         }] 
       }, { 
        "subcat_id": 2, 
        "subcat_name": "Business Licensing", 
         "articles":[{ 
          "article_id": 1, 
          "article_heading": "What is a license?", 
          "article_url": "http://example.com/1" 
         },{ 
          "article_id": 2, 
          "article_heading": "Types of Business Licenses", 
          "article_url": "http://example.com/2" 
         }] 
       }] 
     }, { 
      "cat_id": 2, 
      "cat_name": "Family Law", 
       "subcategories":[{ 
        "subcat_id": 3, 
        "subcat_name": "Domestic Violence", 
         "articles":[{ 
          "article_id": 1, 
          "article_heading": "What is domestic violene?", 
          "article_url": "http://example.com/1" 
         },{ 
          "article_id": 2, 
          "article_heading": "Types of domestic violence", 
          "article_url": "http://example.com/2" 
         }] 
       }, { 
        "subcat_id": 4, 
        "subcat_name": "Marriage", 
         "articles":[{ 
          "article_id": 1, 
          "article_heading": "What is a marriage?", 
          "article_url": "http://example.com/1" 
         },{ 
          "article_id": 2, 
          "article_heading": "Types of marriages", 
          "article_url": "http://example.com/2" 
         }] 
       }] 
     }] 
}] 

}

すべてのカテゴリを表示してリストビューに表示しますが、リストビューで選択したカテゴリのサブカテゴリを表示します。

+2

hava [jsonschema2pojo](http://www.jsonschema2pojo.org/)と[Google Gson](https://github.com/google/gson)を見ると、これは簡単になります –

答えて

0

あなたの助けをみんなありがとう。私はついにこの問題を解決することができた。 ここに私がそれをした方法があります。

JSONObject jsonObj = new JSONObject(jsonStr); 
JSONObject subcategories = jsonObj.getJSONObject("data").getJSONArray("categories").getJSONObject((int)getItemId(position)); 
Log.e("TAG","Subcategories: " + subcategories); 

これは正しい方法かどうかわかりませんが、私の場合はうまくいきました。

0

このリンクにはExpandableListViewexampleを使用してください。 jsonの解析では、Google gsonを使用すると、作業が簡単で効率的になります。

1

リストビューでonItemClickListenerを設定し、選択した項目の位置を使用して関連するサブカテゴリを返しますか?おそらく、例えば

、の線に沿って何か:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Switch(position) { 
        case 0: 
         //get first subcategories array values 
         break; 
        case 1: 
         //get second subcategories array values 
         break; 
        .... 
       } 
      } 
関連する問題