我正在尝试JSON
在我的APP上解析它,然后尝试将结果放到a中TextView
,因为我现在有了JSON
in String
,这是我的JSON
[{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"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"
}
},
.....
我尝试了这个:
JSONArray users = new JSONArray(result);
tvNames.setText(users.get("name"));
但这不起作用。
您正在尝试从a获取名称,JSONArray
并且需要对其进行迭代并JSONObject
使用键“ name”找到,如下所示:
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();
}
然后,您现在可以使用该方法的结果
tvNames.setText()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句