我必須解析下面嵌套的Json數組的數據到我的應用程序。我很困惑如何從中獲得價值。如何解析這個嵌套的JSON數組android
{
"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
@Enrichman」我添加了試用代碼。我也刪除了那個逗號 –