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.
-
establishments should work instead, after that, go insideguillaume girod-vitouchkina– guillaume girod-vitouchkina2015年12月04日 14:39:10 +00:00Commented Dec 4, 2015 at 14:39
-
@guillaumegirod-vitouchkina what's the syntax for accessing establishments then establishment? sorry, very new at this.1.mkn– 1.mkn2015年12月04日 15:24:55 +00:00Commented Dec 4, 2015 at 15:24
-
@guillaume establishments doesn't work either...1.mkn– 1.mkn2015年12月04日 16:07:24 +00:00Commented Dec 4, 2015 at 16:07
3 Answers 3
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.
3 Comments
string == expectedString.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
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"));
}