0

I'm trying to parse the following (expected) json response to return an array of 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"}. I'm also getting the following error: JSONObject["establishment"] not found.

Not really sure what's wrong. Can anyone help? Thanks.

3
  • establishments should work instead, after that, go inside Commented Dec 4, 2015 at 14:39
  • @guillaumegirod-vitouchkina what's the syntax for accessing establishments then establishment? sorry, very new at this. Commented Dec 4, 2015 at 15:24
  • @guillaume establishments doesn't work either... Commented Dec 4, 2015 at 16:07

3 Answers 3

1

You can't access establishment if you didn't go into establishments. For example we have class A which has field with class B. You can't access B until you get 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.

Sign up to request clarification or add additional context in comments.

3 Comments

how can I check my json? when it print it out with toString() I get the UTF8 code though I don't think that's correct. If i convert the response to a string buffer > toString() and print that it's the response that I expect it to be. How do I go into establishments? sorry, pretty new at this.
Try something like string == expectedString.
the json response > String Buffer isn't the same as the json response itself...I don't understand why this is or what I should do about.
0

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

    }

1 Comment

As @mibac said first need to access establishments then loop through all establishment and get ids, before using this code think about performance(object creation in loop and toString operation..!!)
0

First of all , it should be

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

The JSON array key is establishments with the s

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

2 Comments

my code doesn't even make it to the loop, event after changing the array key to "establishments" with the s it still says "[establishments] not found."
Then something is else is not correct, I suggest you log r.toString()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.