Tôi phải phân tích cú pháp dữ liệu mảng Json lồng nhau bên dưới vào ứng dụng của tôi. Tôi đang bối rối làm thế nào để có được các giá trị của nó.Cách phân tích mảng JSON lồng nhau này trong 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"
}
]
},
]
}
Bất kỳ ai cũng có thể hướng dẫn tôi cách lấy các giá trị bên trong từ đó.
Tôi đã cố gắng này
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 ok tôi sẽ cho bạn thấy. –
Btw, json đó không hợp lệ. Mục cuối cùng trong đối tượng bên trong kết thúc bằng dấu phẩy: '" sku ":" 5555 ",' – Enrichman
@Enrichman tôi đã thêm mã đã thử. Ngoài ra tôi đã xóa dấu phẩy –