Reputation: 20538
I am building an Android app and are collecting data from my remote API (using Volley) and I need to parse the response. I know how to get the "projects" array but how can I get the title for each project (please note the project key for each project).
{
"code": 200,
"total": 4,
"projects": [
{
"project": {
"id": 1,
"title": "A nice long title",
"latitude": 56.0293783,
"longitude": 12.7256732,
"created_at": "2013-10-20T20:57:00+02:00",
"created_at_human": "5 months",
"total_tasks": 7,
"description": "This is a description.",
"address": "simple highway 22",
"zipcode": "25656",
"city": "florida"
}
},
{
"project": {
"id": 2,
"title": "A nice long title",
"latitude": 56.0293783,
"longitude": 12.7256732,
"created_at": "2013-10-20T20:57:00+02:00",
"created_at_human": "5 months",
"total_tasks": 7,
"description": "This is a description.",
"address": "simple highway 22",
"zipcode": "25656",
"city": "florida"
}
}
]
}
This is the code I use now and that needs to be modified:
JSONArray jsonPosts = mData.getJSONArray("projects");
ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonPosts.length(); i++) {
JSONObject post = jsonPosts.getJSONObject(i);
Log.e("OUTPUT", "THE POST: " + post);
}
This outputs:
E/OUTPUT﹕ THE POST: {"project":{"id":1,"title":"A nice long title","total_tasks":7,"address":"simple highway 22","description":"This is a description.","zipcode":"25656","created_at":"2013-10-20T20:57:00+02:00","longitude":12.7256732,"created_at_human":"5 MåNADER","latitude":56.0293783,"city":"florida"}}
How can I access the title for each?
Upvotes: 0
Views: 45
Reputation: 5319
Each jSONObject
item contains a key and jSONObject
. I think you need to get jSONObject from item in Array then get title and city.
I hope it will helpful for you........
JSONArray jsonPosts = mData.getJSONArray("projects");
ArrayList<HashMap<String, String>> blogPosts = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonPosts.length(); i++) {
JSONObject post = jsonPosts.getJSONObject(i);
JSONObject innerjson = post.getJSONObject("project");
String title = innerjson.getString(KEY_TITLE);
title = Html.fromHtml(title).toString();
String city = innerjson.getString(KEY_CITY);
city = Html.fromHtml(city).toString();
HashMap<String, String> blogPost = new HashMap<String, String>();
blogPost.put(KEY_TITLE, title);
blogPost.put(KEY_CITY, city);
blogPosts.add(blogPost);
}
String[] keys = {KEY_TITLE, KEY_CITY};
int[] ids = { R.id.top_label, R.id.bottom_label};
SimpleAdapter adapter = new SimpleAdapter(this, blogPosts, R.layout.list_item, keys, ids);
setListAdapter(adapter);
Upvotes: 1
Reputation: 5145
This is a JSONArray
and not a JSONObject
- to make a JSONObject
from it, use
JSONObject jsonObject = jsonArray.getJSONObject(0);
this gets the first JSONObject from this JSONArray.
If you have multiple JSONObjects, use this:
JSONObject jsonObject;
for(int n = 0; n < jsonArray.length(); n++)
{
jsonObject = jsonArray.getJSONObject(n);
}
To get the values:
jsonObject.getString("name");
Upvotes: 1