Yznit Yznit - 3 years ago 187
Android Question

Decode nested json array in android

I have a nested JSON array which i want to decode, i have already find a way deocde a specified info in the object but its not working so good.

This is the code i use to decode (name, cover and street) from the json-object:

JSONArray jsonArray = response.getJSONArray("data");

for (int i=0; i < jsonArray.length();i++){
JSONObject events = jsonArray.getJSONObject(i);
String name = events.getString("name");
JSONObject cover = events.getJSONObject("cover");
String imgurl = cover.getString("source");
JSONObject place = events.getJSONObject("place");
JSONObject loc = place.getJSONObject("location");
String street = loc.getString("street");

ItemListView item = new ItemListView(name,street,imgurl);
listItems.add(item);
adapter = new ItemListAdapter(listItems, getActivity());

recyclerView.setAdapter(adapter);
}


I want to decode more info like description , end_time and start_time.

The json data structure looks like this:

{
"data": [
{
"description": "",
"end_time": "",
"name": "",
"place": {
"name": "",
"location": {
"city": "",
"country": "",
"latitude": 0000000,
"longitude": 000000,
"street": ""
},
"id": ""
},
"start_time": "",
"id": ""
},
{
"description": "",
"end_time": "",
"name": "",
"place": {
"name": "",
"location": {
"city": "",
"country": "",
"latitude": 0000000,
"longitude": 000000,
"street": ""
},
"id": ""
},
"start_time": "",
"id": ""
}

}


I put the decoded info in a listview in my android app and that is why i have adapter and listView in the code.

Any help is greatly appreciated :)
Thanks in advance.

Answer Source

This is simple you can get other string like this -:

JSONArray jsonArray = response.getJSONArray("data");

            for (int i=0; i < jsonArray.length();i++){
                JSONObject events = jsonArray.getJSONObject(i);
                String description= events.getString("description");
                String end_time= events.getString("end_time");
                String start_time= events.getString("start_time");

            }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download