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

时间:2015-12-04 14:37:45

标签: java json api parsing

我试图解析以下(预期的)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"
         }
      }
   ]
}

我使用以下代码:

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打印出1并且r.toString打印出{&#34;编码&#34;:&#34; UTF8&#34;}。我也收到以下错误:找不到JSONObject [&#34;建立&#34;]。

不确定是什么错。有人可以帮忙吗?感谢。

3 个答案:

答案 0 :(得分:1)

如果您没有进入establishment,则无法访问establishments。例如,我们有一个类A,其中包含类B的字段。在获得B之前,您无法访问A。另一个例子:当你吃橙子时,你先吃什么内容或去除皮肤?

你也可以考虑检查你的JSON是什么样的。

答案 1 :(得分:0)

希望这段代码片段可以帮助你...... !!

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

    }

答案 2 :(得分:0)

首先,它应该是

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

JSON数组键为establishmentss

然后因为你的对象嵌套在另一个对象中:

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

你的循环需要改变:

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