Tristan O'Keefe
Tristan O'Keefe

Reputation: 411

How to parse a JSON Object within two JSON Array's?

I'm trying to get the item name, brand name and total carbohydrate value out of the following JSON Array but am having problems with accessing the the individual values within the "fields" section. Anyone with any pointers to retrieve this info?

{

"total_hits": 49127, "max_score": 11.919899, "hits": [ {

  "_index": "f762ef22-e660-434f-9071-a10ea6691c27",
  "_type": "item",
  "_id": "513fceb375b8dbbc21000022",
  "_score": 11.919899,
  "fields": {
    "item_id": "513fceb375b8dbbc21000022",
    "item_name": "Cheese, cheddar - 1 cup, diced",
    "brand_name": "USDA",
    "nf_total_carbohydrate": 4.08,
    "nf_serving_size_qty": 1,
    "nf_serving_size_unit": "serving"
  }
},
{
  "_index": "f762ef22-e660-434f-9071-a10ea6691c27",
  "_type": "item",
  "_id": "513fceb375b8dbbc21000021",
  "_score": 11.788424,
  "fields": {
    "item_id": "513fceb375b8dbbc21000021",
    "item_name": "Cheese, cheddar - 1 cup, melted",
    "brand_name": "USDA",
    "nf_total_carbohydrate": 7.54,
    "nf_serving_size_qty": 1,
    "nf_serving_size_unit": "serving"
  }

/* sorry for some reason i can't get the formatting right but the "hits" is a parent of the whole highlighted code section*/

Upvotes: 0

Views: 110

Answers (2)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Here is the fully working code. Try this:

// Your JSON string
String jsonStr = "{\n" +
            "  \"total_hits\": 49127,\n" +
            "  \"max_score\": 11.919899,\n" +
            "  \"hits\": [\n" +
            "    {\n" +
            "      \"_index\": \"f762ef22-e660-434f-9071-a10ea6691c27\",\n" +
            "      \"_type\": \"item\",\n" +
            "      \"_id\": \"513fceb375b8dbbc21000022\",\n" +
            "      \"_score\": 11.919899,\n" +
            "      \"fields\": {\n" +
            "        \"item_id\": \"513fceb375b8dbbc21000022\",\n" +
            "        \"item_name\": \"Cheese, cheddar - 1 cup, diced\",\n" +
            "        \"brand_name\": \"USDA\",\n" +
            "        \"nf_total_carbohydrate\": 4.08,\n" +
            "        \"nf_serving_size_qty\": 1,\n" +
            "        \"nf_serving_size_unit\": \"serving\"\n" +
            "      }\n" +
            "    },\n" +
            "    {\n" +
            "      \"_index\": \"f762ef22-e660-434f-9071-a10ea6691c27\",\n" +
            "      \"_type\": \"item\",\n" +
            "      \"_id\": \"513fceb375b8dbbc21000021\",\n" +
            "      \"_score\": 11.788424,\n" +
            "      \"fields\": {\n" +
            "        \"item_id\": \"513fceb375b8dbbc21000021\",\n" +
            "        \"item_name\": \"Cheese, cheddar - 1 cup, melted\",\n" +
            "        \"brand_name\": \"USDA\",\n" +
            "        \"nf_total_carbohydrate\": 7.54,\n" +
            "        \"nf_serving_size_qty\": 1,\n" +
            "        \"nf_serving_size_unit\": \"serving\"\n" +
            "      }\n" +
            "    }\n" +
            "  ]\n" +
            "}";

    try {
        JSONObject jsonObject = new JSONObject(jsonStr);
        JSONArray jsonArrayHits = jsonObject.getJSONArray("hits");

        // Get all jsonObject from jsonArray
        for (int i = 0; i < jsonArrayHits.length(); i++)
        {
            JSONObject jsonObjectFields = jsonArrayHits.getJSONObject(i).getJSONObject("fields");

            String itemName = null, brandName = null;
            double totalCarbohydrate = 0.0;

            // Item name
            if (jsonObjectFields.has("item_name") && !jsonObjectFields.isNull("item_name")) {
                itemName = jsonObjectFields.getString("item_name");
            }

            // Brand name
            if (jsonObjectFields.has("brand_name") && !jsonObjectFields.isNull("brand_name")) {
                brandName = jsonObjectFields.getString("brand_name");
            }

            // Total carbohydrate
            if (jsonObjectFields.has("nf_total_carbohydrate") && !jsonObjectFields.isNull("nf_total_carbohydrate")) {
                totalCarbohydrate = jsonObjectFields.getDouble("nf_total_carbohydrate");
            }

            Log.d("SUCCESS", "JSON Object: " + "\nItem Name: " + itemName
                    + "\nBrand Name: " + brandName
                    + "\nTotal carbohydrate: " + totalCarbohydrate);
        }
    } catch (JSONException e) {
        Log.e("FAILED", "Json parsing error: " + e.getMessage());
    }

OUTPUT LOG:

D/SUCCESS: JSON Object: 
           Item Name: Cheese, cheddar - 1 cup, diced
           Brand Name: USDA
           Total carbohydrate: 4.08

D/SUCCESS: JSON Object: 
           Item Name: Cheese, cheddar - 1 cup, melted
           Brand Name: USDA
           Total carbohydrate: 7.54

Hope this will help~

Upvotes: 0

Joe Rakhimov
Joe Rakhimov

Reputation: 5083

Try this:

    try {
        JSONObject object = new JSONObject(json);
        JSONArray hits = object.getJSONArray("hits");
        for (int i = 0; i < hits.length(); i++) {
            JSONObject fields = hits.getJSONObject(i).getJSONObject("fields");
            String itemName = fields.getString("item_name");
            String brandName = fields.getString("brand_name");
            double carbohydrate = fields.getDouble("nf_total_carbohydrate");
            Log.d("HitTag", itemName+" "+brandName+" "+carbohydrate);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

I assume that you have this json:

{
  "total_hits": 49127,
  "max_score": 11.919899,
  "hits": [
    {
      "_index": "f762ef22-e660-434f-9071-a10ea6691c27",
      "_type": "item",
      "_id": "513fceb375b8dbbc21000022",
      "_score": 11.919899,
      "fields": {
        "item_id": "513fceb375b8dbbc21000022",
        "item_name": "Cheese, cheddar - 1 cup, diced",
        "brand_name": "USDA",
        "nf_total_carbohydrate": 4.08,
        "nf_serving_size_qty": 1,
        "nf_serving_size_unit": "serving"
      }
    },
    {
      "_index": "f762ef22-e660-434f-9071-a10ea6691c27",
      "_type": "item",
      "_id": "513fceb375b8dbbc21000021",
      "_score": 11.788424,
      "fields": {
        "item_id": "513fceb375b8dbbc21000021",
        "item_name": "Cheese, cheddar - 1 cup, melted",
        "brand_name": "USDA",
        "nf_total_carbohydrate": 7.54,
        "nf_serving_size_qty": 1,
        "nf_serving_size_unit": "serving"
      }
    }
  ]
}

Upvotes: 1

Related Questions