【问题标题】:how to write code for Json Array Inside Json Array as Category and subcategory with android如何在 Json Array 中为 Json Array 编写代码作为类别和 android 的子类别
【发布时间】:2017-09-16 15:06:14
【问题描述】:

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

类别- DIY 子类别 - 配件 子类别- 花园 等等.. 完整的模式会像 DIY 配件 花园 工具 礼物 装饰性的 父亲节 情人节
{
"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
}}

请任何人帮助我找到正确的编码方式?

【问题讨论】:

标签: android arrays json hashmap expandablelistview


【解决方案1】:

试试这个:

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");

【讨论】:

  • 这是可行的,但它会将数据放入 DESC 顺序并在 recyclerview 中设置,但我想设置数据第一个主类别,然后添加一个子类别,但如何? @KuldeepKulkarni
【解决方案2】:

试试这个:

// 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" +
            "}}";

使用方法parseJSON(sampleJson)从上述sampleJson数据中解析CategorySubCategory值。

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

输出:

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

希望对你有帮助~

【讨论】:

  • 谢谢@Ferdous
  • 非常欢迎 :) .... 如果我的回答对您有所帮助并且看起来很有用,那么您应该接受这个答案并给予支持。请阅读:stackoverflow.com/help/someone-answers
  • 很高兴知道这一点,并感谢您接受我的回答。如果觉得对你有用,请点赞。提前致谢
猜你喜欢
  • 2018-08-23
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多