2016-12-06 162 views
0

我想解析看起來像這樣在android中的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

使用ExpandableListView,example在此鏈接上給出。 對於json解析使用Google gson,它會讓你的工作變得簡單而高效。

0

它看起來類似於How to parse this nested JSON array in android 但是,您可以使用此庫來使用對象使工作變得簡單快捷。 這是一個小教程:JacksonInFiveMinutes 特別是:完整數據綁定(POJO)示例。和JacksonDataBinding

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; 
        .... 
       } 
      }