how to write code for Json Array Inside Json Array

2019-09-20 02:01发布

问题:

My requirement is simple. i have a json pattern like below. i want to help in JSON parsing. as category and subcategory following way.

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

please can any body help me to find the right way to code?

回答1:

Try this:

// Here is your sample JSON
String sampleJson = "{\n" +
            "\"CategoryList\": [{\n" +
            "    \"MainCategory\": {\n" +
            "        \"Attribute_Name\": \"DIY\",\n" +
            "        \"Attribute_Value\": \"125\",\n" +
            "        \"StockKeepingunit\": null\n" +
            "    },\n" +
            "    \"SubCategory\": [{\n" +
            "        \"Attribute_Name\": \"126\",\n" +
            "        \"Attribute_Value\": \"Accessories\",\n" +
            "        \"StockKeepingunit\": null\n" +
            "    }, {\n" +
            "        \"Attribute_Name\": \"127\",\n" +
            "        \"Attribute_Value\": \"Garden\",\n" +
            "        \"StockKeepingunit\": null\n" +
            "    }, {\n" +
            "        \"Attribute_Name\": \"128\",\n" +
            "        \"Attribute_Value\": \"Tools\",\n" +
            "        \"StockKeepingunit\": null\n" +
            "    }]\n" +
            "}, {\n" +
            "    \"MainCategory\": {\n" +
            "        \"Attribute_Name\": \"Gifts\",\n" +
            "        \"Attribute_Value\": \"133\",\n" +
            "        \"StockKeepingunit\": null\n" +
            "    },\n" +
            "    \"SubCategory\": [{\n" +
            "        \"Attribute_Name\": \"134\",\n" +
            "        \"Attribute_Value\": \"Decorative\",\n" +
            "        \"StockKeepingunit\": null\n" +
            "    }, {\n" +
            "        \"Attribute_Name\": \"135\",\n" +
            "        \"Attribute_Value\": \"Fathers day\",\n" +
            "        \"StockKeepingunit\": null\n" +
            "    }, {\n" +
            "        \"Attribute_Name\": \"138\",\n" +
            "        \"Attribute_Value\": \"Valentines Day\",\n" +
            "        \"StockKeepingunit\": null\n" +
            "    }]\n" +
            "\n" +
            "}],\n" +
            "\"status\": {\n" +
            "    \"message\": \"Success\",\n" +
            "    \"result\": 1\n" +
            "}}";

Use method parseJSON(sampleJson) to parse Category and SubCategory values from the above sampleJson data.

public 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());
        }
    }
}

OUTPUT:

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

Hope this will help~



回答2:

Try this:

JSONObject jsonObject = new JSONObject();

                JSONArray jsonArray = jsonObject.getJSONArray("CategoryList");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    JSONObject jsonObject2 = jsonObject1.getJSONObject("MainCategory");
                    String Attribute_Name = jsonObject2.getString("Attribute_Name");
                    String Attribute_Value = jsonObject2.getString("Attribute_Value");
                    String StockKeepingunit = jsonObject2.getString("StockKeepingunit");

                    JSONArray jsonArray1 = jsonObject1.getJSONArray("SubCategory");

                    for (int j = 0; j < jsonArray1.length(); j++) {
                        JSONObject jsonObject3 = jsonArray1.getJSONObject(i);
                        String Attribute_Name1 = jsonObject2.getString("Attribute_Name");
                        String Attribute_Value1 = jsonObject2.getString("Attribute_Value");
                        String StockKeepingunit1 = jsonObject2.getString("StockKeepingunit");

                    }
                }

                JSONObject jsonObject1 = jsonObject.getJSONObject("status");
                String message = jsonObject1.getString("message");
                int result = jsonObject1.getInt("result");