Android json按类别解析

时间:2013-09-23 06:02:58

标签: android json android-layout android-intent android-listview

我有一个如下所示的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中的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);
}

3 个答案:

答案 0 :(得分:3)

在for循环中,使用

检索所需的JSON对象
    JSONObject c = ja.getJSONObject(i);

然后使用

将Json项存储在变量中
    String str = c.getString("category Name");

使用{表示JSON对象。因此,根据您的JSON结构,解析它并检索所需的项目。

答案 1 :(得分:3)

您可以使用HashMap存储单独的类别列表,您可以轻松获得所需的类别列表,如T-e-PBS,Rhymes等。

// 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) {

}

答案 2 :(得分:2)

您可以HashMap使用ArrayLists

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