0

I am newbie learning Android and ran into an issue when trying to parse my API data into my app. It calls fine, but I am unsure why I am getting the error.

Here is the API data that is pulled:

{  
   "report":{  
      "sr":"28",
      "type":"Basic",
      "food":{  
         "ndbno":"01009",
         "name":"Cheese, cheddar",
         "ds":"Standard Reference",
         "manu":"",
         "ru":"g",
         "nutrients":[  
            {  
               "nutrient_id":"255",
               "name":"Water",
               "derivation":"NONE",
               "group":"Proximates",
               "unit":"g",
               "value":"37.02",
               "measures":[  
                  {  
                     "label":"cup, diced",
                     "eqv":132,
                     "eunit":"g",
                     "qty":1,
                     "value":"48.87"
                  }
               ]
            }
         ]
      }
   }
}

And here is my code:

JSONArray JA = new JSONArray(macros);

for (int i =0; i <JA.length(); i++){
    JSONObject JO = (JSONObject) JA.get(i);
    singleParsed = "Name:" + JO.get("report") + "\n" +
            "Item:" + JO.get("reports.food.name") + "\n" +
            "Nutrient:" + JO.get("reports.food.nutrients.name") + "\n" +
            "Serving suggestions:" + JO.get("reports.food.measures.value");

So far that hasn't worked for me.

W/System.err: org.json.JSONException: Value {"report":{"sr":"28","type":"Basic","food":{"ndbno":"01009","name":"Cheese, cheddar","ds":"Standard Reference","manu":"","ru":"g","nutrients":[{"nutrient_id":"255","name":"Water","derivation":"NONE","group":"Proximates","unit":"g","value":"37.02","measures":[{"label":"cup, diced","eqv":132,"eunit":"g","qty":1,"value":"48.87"},{"label":"cup, melted","eqv":244,"eunit":"g","qty":1,"value":"90.33"},{"label":"cup, shredded","eqv":113,"eunit":"g","qty":1,"value":"41.83"},{"label":"oz","eqv":28.35,"eunit":"g","qty":1,"value":"10.50"},{"label":"cubic inch","eqv":17,"eunit":"g","qty":1,"value":"6.29"},{"label":"slice (1 oz)","eqv":28,"eunit":"g","qty":1,"value":"10.37"}]},{"nutrient_id":"208","name":"Energy","derivation":"NC","group":"Proximates","unit":"kcal","value":"404","measures":[{"label":"cup, diced","eqv":132,"eunit":"g","qty":1,"value":"533"},{"label":"cup, melted","eqv":244,"eunit":"g","qty":1,"value":"986"},{"label":"cup, shredded","eqv":113,"eunit":"g","qty":1,"value":"457"},{"label":"oz","eqv":28.35,"eunit":"g","qty":1,"value":"115"},{"label":"cubic inch","eqv":17,"eunit":"g","qty":1,"value":"69"},{"label":"slice (1 oz)","eqv":28,"eunit":"g","qty":1,"value":"113"}]},{"nutrient_id":"203","name":"Protein","derivation":"NONE","group":"Proximates","unit":"g","value":"22.87","measures":[{"label":"cup, diced","eqv":132,"eunit":"g","qty":1,"value":"30.19"},{"label":"cup, melted","eqv":244,"eunit":"g","qty":1,"value":"55.80"},{"label":"cup, shredded","eqv":113,"eunit":"g","qty":1,"value":"25.84"},{"label":"oz","eqv":28.35,"eunit":"g","qty":1,"value":"6.48"},{"label":"cubic inch","eqv":17,"eunit":"g","qty":1,"value":"3.89"},{"label":"slice (1 oz)","eqv":28,"eunit":"g","qty":1,"value":"6.40"}]},{"nutrient_id":"204","name":"Total lipid (fat)","derivation":"NONE","group":"Proximates","unit":"g","value":"33.31","measures":[{"label":"cup, diced","eqv":132,"eunit":"g","qty":1,"value":"43.97"},{"label":"cup, melted","eqv":244,"eunit":"g","qty":1,"value":"81.28"},{"label":"cup, shredded","eqv":113,"eunit":"g","qty":1,"value":"37.64"},{"label":"oz","eqv":28.35,"eunit":"g","qty":1,"value":"9.44"},{"label":"cubic inch","eqv":17,"eunit":"g","qty":1,"value":"5.66"},{"label":"slice (1 oz)","eqv":28,"eunit":"g","qty":1,"value":"9.33"}]},{"nutrient_id":"205","name":"Carbohydrate, by difference","derivation":"NC","group":"Proximates","unit":"g","value":"3.09","measures":[{"label":"cup, diced","eqv":132,"eunit":"g","qty":1,"value":"4.08"},{"label":"cup, melted","eqv":244,"eunit":"g","qty":1,"value":"7.54"},{"label":"cup, shredded","eqv":113,"eunit":"g","qty":1,"value":"3.49"},{"label":"oz","eqv":28.35,"eunit":"g","qty":1,"value":"0.88"},{"label":"cubic inch","eqv":17,"eunit":"g","qty":1,"value":"0.53"},{"label":"slice (1 oz)","eqv":28,"eunit":"g","qty":1,"value":"0.87"}]},{"nutrient_id":"291","name":"Fiber, total dietary","derivation":"NONE","group":"Proximates","unit":"g","value":"0.0","measures":[{"label":"cup, diced","eqv":132,"eunit":"g","qty":1,"value":"0.0"},{"label":"cup, melted","eqv":244,"eunit":"g","qty":1,"value":"0.0"},{"label":"cup, shredded","eqv":113,"eunit":"g","qty":1,"value":"0.0"},{"label":"oz","eqv":28.35,"eunit":"g","qty":1,"value":"0.0"},{"label":"cubic inch","eqv":17,"eunit":"g","qty":1,"value":"0.0"},{"label":"slice (1 oz)","eqv":28,"eunit":"g","qty":1,"value":"0.0"}]},{"nutrient_id":"269","name":"Sugars, total","derivation":"AS","group":"Proximates","unit":"g","value":"0.48","measures":[{"label":"cup, diced","eqv":132,"eunit":"g","qty":1,"value":"0.63"},{"label":"cup, melted","eqv":244,"eunit":"g","qty":1,"value":"1.17"},{"label":"cup, shredded","eqv":113,"eunit":"g","qty":1,"value":"0.54"},{"label":"oz","eqv":28.35,"eunit":"g","qty":1,"value":"0.14"},{"label":"cubic inch","eqv":17,"eunit":"g","qty":1,"value":"0.08"},{"label":"slice (1 oz)","eqv":28,"eunit":"g","qty":1,"value":"0.13"}]},{"nutrient_id":"301","name":"Calcium, Ca","derivation":"NONE","group":"Minerals","unit":"mg","value":"710","measures":[{"label":"cup, diced","eqv":132,"eunit":"g","q
              at org.json.JSON.typeMismatch(JSON.java:111)
              at org.json.JSONArray.<init>(JSONArray.java:96)
              at org.json.JSONArray.<init>(JSONArray.java:108)
              at com.monreal.deb.macrocalculator.fetchData.doInBackground(fetchData.java:38)
              at com.monreal.deb.macrocalculator.fetchData.doInBackground(fetchData.java:17)
              at android.os.AsyncTask$2.call(AsyncTask.java:333)
              at java.util.concurrent.FutureTask.run(FutureTask.java:266)
              at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
              at java.lang.Thread.run(Thread.java:764)

I am extremely grateful for any help that you can provide.

Deb
  • 59
  • 1
  • 7
  • 1
    Possible duplicate of [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Selvin Mar 30 '18 at 19:54

1 Answers1

1

Use Gson for parsing JSON. Its simpler and easier to implement. You just have to define some classes which will contain the parsed information from your JSON. From your JSON structure, you need to have the following classes.

Report.java

public class Report {
    public Food food;
    public String sr;
    public String type;
}

Then Food.java

public class Food {
    public Nutrients[] nutrients;
    public String manu;
    public String name;
    public String ndbno;
    public String ru;
    public String ds;
}

Nutrients.java

public class Nutrients {
    public String unit;
    public String derivation;
    public String name;
    public String nutrient_id;
    public String value;
    public Measures[] measures;
    public String group;
}

And finally, Measures.java

public class Measures {
    public String eqv;
    public String eunit;
    public String value;
    public String qty;
    public String label;
}

Once you have these classes in your project, you are now ready to parse the JSON data using Gson with the following.

Gson gson = new Gson();
Report report = gson.fromJson(yourJsonString, Report.class);

To use Gson, you need to add the following dependency, in your build.gradle file.

dependencies {
    compile 'com.google.code.gson:gson:2.8.2'
}

Hope that helps!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98