I'm trying to parse this JSON
on my APP and trying to put the result on a TextView
from now I have the JSON
in a String
, this is my JSON
[{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
.....
I tried this:
JSONArray users = new JSONArray(result);
tvNames.setText(users.get("name"));
But it's not working.
You're trying to get the name from a JSONArray
and you need to iterate through it and find the JSONObject
with the key "name", as follow :
private String getAllNames(String result){
StringBuilder names = new StringBuilder();
try{
JSONArray users = new JSONArray(result);
for(int i = 0; i<users.length();i++){
JSONObject user = users.getJSONObject(i);
names.append(user.getString("name")).append("\n");
}
}catch(JSONException ex){
Log.d("Debug","Error parsing json " + ex.getMessage());
}
return names.toString();
}
Then you can do now with the result of that method
tvNames.setText()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.