【发布时间】:2013-07-14 10:16:39
【问题描述】:
我必须将下面嵌套的 Json 数组的数据解析到我的应用程序中。我很困惑如何从中获得价值。
{
"prodCat_list": [
{
"prods": [
{
"cat_id": "9",
"position": "1",
"sku": "wwww345"
},
{
"cat_id": "9",
"position": "2",
"sku": "coof23"
},
{
"cat_id": "9",
"position": "3",
"sku": "dde45"
},
{
"cat_id": "9",
"position": "4",
"sku": "5555"
}
]
},
{
"prods": [
{
"cat_id": "9",
"position": "1",
"sku": "wwww345"
},
{
"cat_id": "9",
"position": "2",
"sku": "coof23"
},
{
"cat_id": "9",
"position": "3",
"sku": "dde45"
},
{
"cat_id": "9",
"position": "4",
"sku": "5555"
}
]
},
]
}
谁能指导我如何从中获得内部价值。
我试过了
JSONParser parser = new JSONParser();
JSONObject items = parser.getJSONFromUrl(productInfoUrl);
try {
JSONArray itemsDetails = items.getJSONArray("prodCat_list");
if(itemsDetails.length()>0){
for (int i = 0; i < itemsDetails.length(); i++) {
JSONArray productWithCategories = itemsDetails.getJSONArray(i);
JSONObject object = productWithCategories.getJSONObject(i);
Product productInfo = new Product( object.getString("sku"), object.getInt("cat_id"), object.getInt("position"));
ProductDbHandler productDbHandler = new ProductDbHandler(context);
productDbHandler.addProducts(productInfo);
}
}
else
System.out.println("No product to add");
} catch (JSONException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
@Enrichman 好的,我会告诉你的。
-
顺便说一句,那个 json 无效。内部对象中的最后一项以逗号结尾:
"sku": "5555", -
@Enrichman 我已经添加了尝试过的代码。我也删除了那个逗号
-
仍然无效。您的对象末尾有一个逗号。使用 jsonlint.com 检查您的 json。并检查 logcat 是否有任何错误。
-
您的 json 文件中还有一个逗号 (,)(倒数第三行),请将其删除并重试..