简体   繁体   English

在Java中解析json对象,找不到对象

[英]Parsing json object in java, object not found

I'm trying to parse the following (expected) json response to return an array of id 我正在尝试解析以下(预期)json响应以返回ID数组

{
   "establishments":[
      {
         "establishment":{
            "id":21,
            "name":"Quick Bites"
         }
      },
      {
         "establishment":{
            "id":16,
            "name":"Casual Dining"
         }
      },
      {
         "establishment":{
            "id":7,
            "name":"Bar"
         }
      },
      {
         "establishment":{
            "id":6,
            "name":"Pub"
         }
      },
      {
         "establishment":{
            "id":5,
            "name":"Lounge"
         }
      },
      {
         "establishment":{
            "id":31,
            "name":"Bakery"
         }
      },
      {
         "establishment":{
            "id":18,
            "name":"Fine Dining"
         }
      },
      {
         "establishment":{
            "id":275,
            "name":"Pizzeria"
         }
      },
      {
         "establishment":{
            "id":1,
            "name":"Caf\u00e9"
         }
      },
      {
         "establishment":{
            "id":24,
            "name":"Deli"
         }
      },
      {
         "establishment":{
            "id":285,
            "name":"Fast Casual"
         }
      },
      {
         "establishment":{
            "id":271,
            "name":"Sandwich Shop"
         }
      },
      {
         "establishment":{
            "id":282,
            "name":"Taqueria"
         }
      },
      {
         "establishment":{
            "id":283,
            "name":"Brewery"
         }
      },
      {
         "establishment":{
            "id":161,
            "name":"Microbrewery"
         }
      },
      {
         "establishment":{
            "id":23,
            "name":"Dessert Parlour"
         }
      },
      {
         "establishment":{
            "id":101,
            "name":"Diner"
         }
      },
      {
         "establishment":{
            "id":286,
            "name":"Coffee Shop"
         }
      },
      {
         "establishment":{
            "id":81,
            "name":"Food Truck"
         }
      },
      {
         "establishment":{
            "id":91,
            "name":"Bistro"
         }
      },
      {
         "establishment":{
            "id":272,
            "name":"Cocktail Bar"
         }
      },
      {
         "establishment":{
            "id":284,
            "name":"Juice Bar"
         }
      },
      {
         "establishment":{
            "id":281,
            "name":"Fast Food"
         }
      },
      {
         "establishment":{
            "id":8,
            "name":"Club"
         }
      },
      {
         "establishment":{
            "id":20,
            "name":"Food Court"
         }
      },
      {
         "establishment":{
            "id":278,
            "name":"Wine Bar"
         }
      }
   ]
}

I'm using the following code: 我正在使用以下代码:

private static void parseEstablishments (JSONObject r) {

    System.out.println("in parseEstablishments");
    System.out.println(r.length()); 


    JSONArray array = r.getJSONArray("establishment");

    List<String> list = new ArrayList<String>();

    for (int i = 0; i < array.length(); i++) {
        list.add(array.getJSONObject(i).getString("id"));
    }


    System.out.println("done");

}

r.length prints out 1 and r.toString prints out {"encoding":"UTF8"}. r.length输出1,r.toString输出{“ encoding”:“ UTF8”}。 I'm also getting the following error: JSONObject["establishment"] not found. 我也收到以下错误:找不到JSONObject [“ Establishment”]。

Not really sure what's wrong. 不太确定出什么问题了。 Can anyone help? 有人可以帮忙吗? Thanks. 谢谢。

You can't access establishment if you didn't go into establishments . 如果您未进入establishment则无法访问establishments For example we have class A which has field with class B . 例如,我们有AA其中的字段为B类。 You can't access B until you get A . 您不能访问B ,直到你得到A Another example: When you eat orange you first eat what's inside or remove the skin? 另一个例子:吃橙子时,首先要吃里面的东西或去除皮肤?

You might also consider checking is your JSON what you expect it to be. 您可能还考虑检查您的JSON是否符合您的期望。

Hope this code snippet helps you..!! 希望此代码段可以帮助您.. !!

private static void parseEstablishments (JSONObject r) throws JSONException {
        System.out.println("in parseEstablishments");
        System.out.println(r.length()); 

        JSONArray array = r.getJSONArray("establishments");
        List<String> list = new ArrayList<String>();

        for (int i = 0; i < array.length(); i++) {
             JSONObject establishmentObj=new JSONObject(array.get(i).toString());
            list.add(String.valueOf(new JSONObject(establishmentObj.get("establishment").toString()).get("id")));
        }
        System.out.println("List Of Ids:"+list);
        System.out.println("done");

    }

First of all , it should be 首先,应该

JSONArray array = r.getJSONArray("establishments");

The JSON array key is establishments with the s JSON数组键是带有s establishments

Then because your object is nested in another object : 然后,因为您的对象嵌套在另一个对象中:

 {
     "establishment":{
        "id":21,
        "name":"Quick Bites"
     }
  }

Your loop needs to change : 您的循环需要更改:

for (int i = 0; i < array.length(); i++) {
    JSONObject establishmentObj=array.getJSONObject(i)
                                     .getJSONObject("establishment");
    list.add(establishmentObj.getString("id"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM