如何将Json Array Inside Json Array的代码编写为带有android的Category和子类别

时间:2017-04-20 06:25:12

标签: android arrays json hashmap expandablelistview

我的要求很简单。我有一个像下面的json模式。我想帮助JSON解析。作为类别和子类别的跟随方式。

category-  DIY
subcategory-  Accessories
subcategory-  Garden 
etc..
full pattern will be like 
   DIY
      Accessories
      Garden
      Tools
   Gifts
      Decorative
      Fathers day
      Valentines Day
{
"CategoryList": [{
    "MainCategory": {
        "Attribute_Name": "DIY",
        "Attribute_Value": "125",
        "StockKeepingunit": null
    },
    "SubCategory": [{
        "Attribute_Name": "126",
        "Attribute_Value": "Accessories",
        "StockKeepingunit": null
    }, {
        "Attribute_Name": "127",
        "Attribute_Value": "Garden",
        "StockKeepingunit": null
    }, {
        "Attribute_Name": "128",
        "Attribute_Value": "Tools",
        "StockKeepingunit": null
    }]
}, {
    "MainCategory": {
        "Attribute_Name": "Gifts",
        "Attribute_Value": "133",
        "StockKeepingunit": null
    },
    "SubCategory": [{
        "Attribute_Name": "134",
        "Attribute_Value": "Decorative",
        "StockKeepingunit": null
    }, {
        "Attribute_Name": "135",
        "Attribute_Value": "Fathers day",
        "StockKeepingunit": null
    }, {
        "Attribute_Name": "138",
        "Attribute_Value": "Valentines Day",
        "StockKeepingunit": null
    }]

}],
"status": {
    "message": "Success",
    "result": 1
}}

请任何人帮我找到合适的编码方式吗?

2 个答案:

答案 0 :(得分:2)

试试这个:

require

答案 1 :(得分:1)

试试这个:

parseJSON(sampleJson)

使用方法Category来解析上述SubCategory数据中的sampleJsonpublic void parseJSON(String jsonStr) { if (jsonStr != null) { try { JSONObject jsonObj = new JSONObject(jsonStr); // CategoryList JSONArray categoryListJsonArray = jsonObj.getJSONArray("CategoryList"); for(int i = 0; i < categoryListJsonArray.length(); i++) { // Category JSONObject categoryJsonObject = categoryListJsonArray.getJSONObject(i); // MainCategory JSONObject mainCategoryJsonObject = categoryJsonObject.getJSONObject("MainCategory"); // Attribute Name String attributeName = mainCategoryJsonObject.getString("Attribute_Name"); Log.d("SUCCESS", "Category: " + attributeName); // SubCategory List JSONArray subCategoryListJsonArray = categoryJsonObject.getJSONArray("SubCategory"); for (int j = 0; j < subCategoryListJsonArray.length(); j++) { // SubCategory JSONObject subCategoryJsonObject = subCategoryListJsonArray.getJSONObject(j); // Attribute Value String attributeValue = subCategoryJsonObject.getString("Attribute_Value"); Log.d("SUCCESS", "Subcategory: " + attributeValue); } } } catch (final JSONException e) { Log.e("FAILED", "Json parsing error: " + e.getMessage()); } } } 值。

D/SUCCESS: Category: DIY
D/SUCCESS: Subcategory: Accessories
D/SUCCESS: Subcategory: Garden
D/SUCCESS: Subcategory: Tools
D/SUCCESS: Category: Gifts
D/SUCCESS: Subcategory: Decorative
D/SUCCESS: Subcategory: Fathers day
D/SUCCESS: Subcategory: Valentines Day

<强>输出:

{{1}}

希望这会有所帮助〜