如何为 Json Array Inside Json Array as Category and subcategory with android 编写代码
how to write code for Json Array Inside Json Array as Category and subcategory with android
我的要求很简单。我有一个 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
}}
任何人都可以帮助我找到正确的编码方式吗?
试试这个:
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");
试试这个:
// 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
数据中的 Category
和 SubCategory
值。
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
希望对你有所帮助~
我的要求很简单。我有一个 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
}}
任何人都可以帮助我找到正确的编码方式吗?
试试这个:
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");
试试这个:
// 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
数据中的 Category
和 SubCategory
值。
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
希望对你有所帮助~