【发布时间】:2015-04-27 15:51:48
【问题描述】:
我可以知道如何从 json 数组中获取 JSON 对象吗??
JSON:
[
{
"id": 1,
"user": {
"id": 20710,
"username": "abc",
"first_name": "",
"last_name": "",
},
"action": {
"name": "xxx",
"date": 19/01/01,
},
},
{
"id": 2,
"user": {
"username": "xyx",
"first_name": "xxx",
"last_name": "yyy",
},
"action": {
"name": "xxx",
"date": 19/05/01,
},
},]
我想在列表中获取这些用户的用户名,但是当我从 api 获取此 json 作为 JSONArray 时,我无法获取值。
我的代码:
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
Log.d("Create Response", response.toString());
ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = response;
try {
if (jsonArray != null) {
int len = jsonArray.length();
for (int i=0;i<len;i++){
JSONObject c=response.getJSONObject(1);
String id = c.getString(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", id);
feeds_List.add(map);
}
}
我无法使用
JSONObject object=JsonArray.getJSONObject("user");
这里,
它只接受 getJSONObject("user"); 的 int 如果它是 JSONArray
【问题讨论】:
-
您是否设法将 JSON 解析为 JSON 数组?如果是,你的下一个问题是什么,如果不是,为什么不呢?
-
@Smutje 我可以将 json 放入 json 数组中,但是我不知道如何在其中获取 json 对象,1)我可以获取 id 之类的值,但无法获取对象像“user”和“action”,因为它是嵌套的。