0

我有一個JSON數組象下面這樣:的Android JSON按類別分析明智

[{"item":{"category Name":"T-e-PBS","SubCategory Name":"T-e-PBS"}}, 
{"item":{"category Name":"T-e-PBS","SubCategory Name":"Animals"}}, 
{"item":{"category Name":"T-e-PBS","SubCategory Name":"Birds"}}, 
{"item":{"category Name":"T-e-PBS","SubCategory Name":"Vegetables"}}, 
{"item":{"category Name":"T-e-PBS","SubCategory Name":"Colors"}}, 
{"item":{"category Name":"Rhymes","SubCategory Name":"Rhymes"}}, 
{"item":{"category Name":"Rhymes","SubCategory Name":"Animated Rhymes"}}, 
{"item":{"category Name":"Rhymes","SubCategory Name":"Cartoon Rhymes"}}, 
{"item":{"category Name":"Rhymes","SubCategory Name":"Prayers"}}] 

我都試過,但不知道如何分類明智的信息,我有很多子類別的類別下:

我想解析這個JSON字符串並填充數據根據類別。

如果我點擊T-e-PBS按鈕,我應該可以在gridview中像GridView中的AnimalsImage,BirdsImages等那樣獲取所有子類別。

如果我點擊Rhymes類別,我應該可以在GridView中獲取所有子類別。

任何人都可以幫忙嗎?

我曾嘗試:

JSONArray ja = new JSONArray(json); 
int size = ja.length(); 
Log.d("tag","No of Elements " + ja.length()); 
for (int i = 0; i < size; i++) { 
    String str = ja.getString(i); 
} 

回答

2

可以使用ArrayListsHashMap

HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>(); 
    try { 

     JSONArray array = new JSONArray(str); 
     // Where str is your response in String form 
     for (int i=0; i<array.length(); i++){ 
      String category = item.getString("category Name"); 
      String subcategory = item.getString("SubCategory Name"); 
      ArrayList<String> categoryFromMap = map.get("category"); 
      if (categoryFromMap == null){ 
       // Not initiated 
       categoryFromMap = new ArrayList<String>(); 
      } 
      categoryFromMap.put(subcategory); 
      map.put(category, categoryFromMap); 
     } 
    } catch (JSONException ex){ 
     ex.printStackTrace(); 
    } 
// Accessing your data 
for (String key : map.keySet()){ 
    // key contains your category names 
    ArrayList<String> subcategories = map.get(key); 
    Log.d("CATEGORY", key); 
    for (String subcat : subcategories){ 
     Log.d("SUBCATEGORY", subcat); 
    } 
} 
// Getting a single category 
ArrayList<String> rhymes = map.get("Rhymes"); 

的Log.d句子應該給予這樣的輸出:

CATEGORY T-e-PBS 
SUBCATEGORY T-e-PBS 
SUBCATEGORY Animals 
SUBCATEGORY Birds 
SUBCATEGORY Vegetables 
SUBCATEGORY Colors 
CATEGORY Rhymes 
SUBCATEGORY Rhymes 
SUBCATEGORY Animated Rhymes 
SUBCATEGORY Cartoon Rhymes 
SUBCATEGORY Prayers 
3

在for循環中,利用

JSONObject c = ja.getJSONObject(i); 

而且在使用一個變量,然後商店的Json項目獲取所需的JSON對象,

String str = c.getString("category Name"); 

JSON對象使用{來表示。因此,根據您的JSON結構,解析它並檢索所需的項目。

3

您可以使用HashMap來存儲不同的類別列表,你可以很容易得到這樣的特-PBS,韻類別的期望列表等

// Which hold the category basis of category Type 
HashMap<String, List<String>> category = new HashMap<String, List<String>>(); 
List<String> cat_name = new ArrayList<String>(); 
List<String> subcat_name = new ArrayList<String>(); 
try { 
    JSONObject script = new JSONObject("YOUR RESPONSE STRING"); 
    JSONArray listOfCategory = script.getJSONArray("YOUR_ARRAY"); 

    for (int j = 0; j < listOfCategory.length(); j++) { 
     JSONObject item = listOfCategory.getJSONObject(j).getJSONObject("item"); 
     String cat = item.getString("category Name"); 
     if (cat.equalsIgnoreCase("T-e-PBS")) { 
      cat_name.add(item.getString("SubCategory Name")); 
     } else if (cat.equalsIgnoreCase("Rhymes")) { 
      subcat_name.add(item.getString("SubCategory Name")); 
     } 
    } 
    category.put("T-e-PBS", cat_name); 
    category.put("Rhymes", subcat_name); 

    // To get the according to Category Name 
    List<String> retrieveCatList1 = category.get("T-e-PBS");//Key is T-e-PBS 
    List<String> retrieveCatList2 = category.get("Rhymes");//Key is Rhyme 
} catch (Exception e) { 

} 
+0

如果您有兩個以上的子類別,該怎麼辦?你會添加他們代碼與if子句? – razielsarafan

+0

@ santirivera92我給出了一種解決問題的方法..我們可以通過多種方式來實現......我們可以動態地存儲多個類別。 –