Vaishali Goel Vaishali Goel - 4 months ago 12
JSON Question

parsing json array with in the array through GSON

I am new in android though I am facing problem in parsing json array with in the array.

{
"menu": [
{
"Soups": [
{
"name": "Safed Tamatar aur Tulsi ",
"price": 150,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Dum Murg",
"price": 168,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Murg Zaffrani",
"price": 168,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Tomato Dhania",
"price": 150,
"description": "",
"veg": true,
"spicy": null
}
]
},
{
"Starters": [
{
"name": "Achari Paneer tikka",
"price": 347,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Paneer Tikka",
"price": 347,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Murg Tikka",
"price": 393,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Tandoori Murg (Half)",
"price": 410,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Tandoori Murg (Full)",
"price": 851,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Macchi Tikka",
"price": 626,
"description": "",
"veg": false,
"spicy": null
}
]
},
{
"Main Course": [
{
"name": "Jeera Aloo",
"price": 275,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Paneer Tikka Masala",
"price": 392,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Palak Paneer",
"price": 370,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Murg Kadhai",
"price": 428,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Murg Tikka Masala",
"price": 428,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Palak Gosht",
"price": 455,
"description": "",
"veg": false,
"spicy": null
},
{
"name": "Macchi Kadhai",
"price": 545,
"description": "",
"veg": false,
"spicy": null
}
]
},
{
"Rice": []
},
{
"Breads": []
},
{
"Accompaniments": []
},
{
"Beverages": []
},
{
"Dessert": []
},
{
"Signature Mocktails": []
},
{
"Wraps": []
},
{
"Lentils": [
{
"name": "Dal Tadka",
"price": 226,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Dal Kolhapuri",
"price": 226,
"description": "",
"veg": true,
"spicy": null
},
{
"name": "Dal Makhani",
"price": 275,
"description": "",
"veg": true,
"spicy": null
}
]
}
],
"name": "Zaffran"
}


I want to parse this file, but there is no such code on internet to parse this file through GSON.
I used following code:

JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue();
JSONArray jsonArray = jsonObject.getJSONArray("menu");
String jsonString = jsonArray.toString();
Type type = new TypeToken<Map<String, String>>() {
}.getType();
Map<String, String> myMap = new Gson().fromJson(jsonString, type);
for (Map.Entry<String, String> entry : myMap.entrySet()) {
key = entry.getKey();
value = entry.getValue();
// do stuff
}
responseView.setText(value);


But, It gives error that String expected found array. Please help me

Answer

Use below code to parse your full json data.

  try {
        JSONObject jsonObject = new JSONObject(jsonArray);

        JSONArray jsonArray1 = jsonObject.getJSONArray("menu");
        Menu menu = new Menu();
        if (jsonObject.has("name")) {
            menu.setName(jsonObject.getString("name"));
        } else {
            menu.setName("");
        }
        List<MenuHeaders> menuHeadersList = new ArrayList<>();
        for (int i = 0; i < jsonArray1.length(); i++) {
            JSONObject jsonObject1 = jsonArray1.getJSONObject(i);
            MenuHeaders menuHeaders = new MenuHeaders();
            //Creating list to save all prices and item names
            List<ItemDetails> list = new ArrayList<>();
            JSONArray subHeaderJsonArray = null;

            if (jsonObject1.has("Soups")) {
                menuHeaders.setTitle("Soups");
                subHeaderJsonArray = jsonObject1.getJSONArray("Soups");
            }
            if (jsonObject1.has("Starters")) {
                menuHeaders.setTitle("Starters");
                subHeaderJsonArray = jsonObject1.getJSONArray("Starters");

            }
            if (jsonObject1.has("Main Course")) {
                menuHeaders.setTitle("Main Course");
                subHeaderJsonArray = jsonObject1.getJSONArray("Main Course");
            }
            if (jsonObject1.has("Rice")) {
                menuHeaders.setTitle("Rice");
                subHeaderJsonArray = jsonObject1.getJSONArray("Rice");
            }
            if (jsonObject1.has("Breads")) {
                menuHeaders.setTitle("Breads");
                subHeaderJsonArray = jsonObject1.getJSONArray("Breads");
            }
            if (jsonObject1.has("Accompaniments")) {
                menuHeaders.setTitle("Accompaniments");
                subHeaderJsonArray = jsonObject1.getJSONArray("Accompaniments");
            }
            //Add all other sub menu items to menu object

            if (jsonObject1.has("Lentils")) {
                menuHeaders.setTitle("Lentils");
                subHeaderJsonArray = jsonObject1.getJSONArray("Lentils");
            }
            if (subHeaderJsonArray != null) {
                for (int index1 = 0; index1 < subHeaderJsonArray.length(); index1++) {
                    JSONObject subHeaderJsonObject = subHeaderJsonArray.getJSONObject(i);
                    ItemDetails itemDetails = new ItemDetails();
                    itemDetails.setName(subHeaderJsonObject.getString("name"));
                    itemDetails.setPrice(subHeaderJsonObject.getInt("price"));
                    itemDetails.setDescription(subHeaderJsonObject.getString("description"));
                    itemDetails.setVeg(subHeaderJsonObject.getBoolean("veg"));
                    itemDetails.setSpicy(subHeaderJsonObject.getString("spicy"));
                    list.add(itemDetails);
                }
                menuHeaders.setList(list);
            }
            menuHeadersList.add(menuHeaders);
            menu.setMenuHeaders(menuHeadersList);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
Comments