Skip to content Skip to sidebar Skip to footer

Android: Parse 2 JsonArray

How i can parse 2 json Array in Android? Plz see the following code; { 'detail': [ { 'price': 51, 'numsc': 2, 'name': 'this app is about animals',

Solution 1:

JSONObject object  = new JSONObject(your-string);

    JSONArray details=object.getJSONArray("details");
for(int j=0;j<details.length();j++){
       JSONObject detail= details.getJSONObject(i);
    String price = detail.getString("price");
    ....
    }
    JSONArray colors = object.getJSONArray("colors");

    for(int i=0;i<colors.length();i++){
       JSONObject obj= colors.getJSONObject(i);
       // parse your json here
    String color = obj.getString("color")

    }

Solution 2:

See following code:

private void decodeJSON()
    {
        String JSONString = "you json String";
        try
        {
            JSONObject obj = new JSONObject(JSONString);
            JSONArray arr = obj.getJSONArray("detail");
            JSONObject detail = arr.getJSONObject(0);
            int price = detail.getInt("price"); // do same thing to get other values


            arr = obj.getJSONArray("colors");
            JSONObject color = arr.getJSONObject(0);
            String colorValue = color.getString("color");
            String name = color.getString("name");

            // do same thing for next object in array.
        } catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

Post a Comment for "Android: Parse 2 JsonArray"