解析深层嵌套的JSON数据

时间:2018-03-30 19:43:34

标签: android json parsing

我是新手学习Android并在尝试将我的API数据解析到我的应用时遇到了问题。它调用很好,但我不确定为什么我收到错误。

以下是提取的API数据:

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

这是我的代码:

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");

到目前为止,这对我没用。

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)

我非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

使用Gson解析JSON。它更简单,更容易实现。您只需要定义一些类,这些类将包含来自JSON的已解析信息。从您的JSON结构中,您需要具有以下类。

Report.java

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

然后是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;
}

最后,Measures.java

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

在项目中有这些类后,您现在可以使用Gson解析JSON数据了。

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

要使用Gson,您需要在build.gradle文件中添加以下依赖项。

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

希望有所帮助!