我的 Java Android 代码中的 Tastypie/Django JSON 响应有问题。我对 Tastypie API 执行 GET http 方法,并将 httpresponse 解析为 JSON。问题在于,当我使用 json 响应创建 JSONArray 时,会抛出异常。
JSONTokener tokener = new JSONTokener(json);
finalResult = new JSONArray(json);
我收到下一条错误消息:
System.err(27193): org.json.JSONException: Value {"objects
[{"id":"1","resource_uri":"\/api\/v1\/user\/1\/","od_user":"Usuario administrador del
sitio","nick":"Admin","reg_date":"2012-08-07T15:39:20.706060+00:00"},
{"id":"2","resource_uri":"\/api\/v1\/user\/2\/","od_user":"user
test","nick":"test1","reg_date":"2012-08-08T10:44:50+00:00"}],"meta":
{"limit":20,"previous":null,"offset":0,"total_count":2,"next":null}} of type
org.json.JSONObject cannot be converted to JSONArray
W/System.err(27193): at org.json.JSON.typeMismatch(JSON.java:111)
W/System.err(27193): at org.json.JSONArray.<init>(JSONArray.java:91)
我从 sweetpie API 获得的 JSON 档案是:
{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 2}, "objects": [{"id": "1", "nick": "Admin", "od_user": "Usuario administrador del sitio", "reg_date": "2012-08-07T15:39:20.706060+00:00", "resource_uri": "/api/v1/user/1/"}, {"id": "2", "nick": "test1", "od_user": "user test", "reg_date": "2012-08-08T10:44:50+00:00", "resource_uri": "/api/v1/user/2/"}]}
我不知道为什么 sweetpie API JSON 格式无法解析为 JSONArray。