0

我有一个如下的 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 字符串并根据类别填充数据。

如果我单击 Te-PBS 按钮,我应该能够在 gridview 中获取所有子类别,如 AnimalsImage、BirdsImages 等。

如果我单击押韵类别,我应该能够在网格视图中获得所有子类别。

有人可以帮忙吗?

我努力了:

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);
}
4

3 回答 3

3

在 for 循环中,使用检索所需的 JSON 对象

    JSONObject c = ja.getJSONObject(i);

然后使用将 Json 项存储在变量中,

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

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

于 2013-09-23T06:25:14.740 回答
3

您可以使用 HashMap 来存储单独的类别列表,您可以轻松获得所需的类别列表,如 Te-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) {

}
于 2013-09-23T06:41:52.167 回答
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
于 2013-09-23T06:53:22.433 回答