Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse this nested JSON array in android

I have to parse the below nested Json array's data into my application. I am confused how to get the values out of it.

  {
            "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"

                        }
             ]
                },
            ]
        }

Can anyone please guide me how to get the inside values from that.

I have tried this

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();
        }
    }
like image 348
Helping_Hand Avatar asked Jul 16 '13 09:07

Helping_Hand


2 Answers

Here is how I think your JSON Parser should look like (there can be some typo mistakes,I didn't test this code on editor : )) :

JSONObject mainObj = new JSONOBject(myString);
if(mainObj != null){
    JSONArray list = mainObj.getJSONArray("prodCat_list");
    if(list != null){
        for(int i = 0; i < list.length();i++){
            JSONObject elem = list.getJSONObject(i);
            if(elem != null){
                JSONArray prods = elem.getJSONArray("prods");
                if(prods != null){
                    for(int j = 0; j < prods.length();j++){
                        JSONObject innerElem = prods.getJSONObject(j);
                        if(innerElem != null){
                            int cat_id = innerELem.getInt("cat_id");
                            int pos = innerElem.getInt("position");
                            String sku = innerElem.getString("sku");
                        }
                    }
                }
            }
        }
    }
}
like image 106
hardartcore Avatar answered Oct 07 '22 08:10

hardartcore


You can use the GSon library. It parses the json into various objects that you can access. A dummy Code is here:

` GSon gSon  = new GSon();
  ProdCatList prodCatList = gSon.fromJson(---inputStreamReader of your JSon data---,ProdCatList.class);`
like image 27
Abhishek Shukla Avatar answered Oct 07 '22 09:10

Abhishek Shukla