Reputation: 1
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.
Upvotes: 0
Views: 2275
Reputation: 45490
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"));
}
Upvotes: 0
Reputation: 26
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");
}
Upvotes: 0
Reputation: 9448
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.
Upvotes: 1