我失去了4小时来设置Android应用程序显示到列表视图的对象或数组,不幸的是结果为空。 我想获得:解析对象和数组到ListView android
- 帖子 - > ID,URL,标题和内容,然后
- 类别 - > ID &冠军,而去年
- 附件 - >图像 - >全 - >网址
.......
{
"status": "ok",
"count": 4,
"count_total": 4,
"pages": 1,
"posts": [
{
"id": 16,
"url": "url,
"status": "publish",
"title": "2014 Yamaha FZ1",
"title_plain": "2014 Yamaha FZ1",
"content": "",
"categories": [
{
"id": 1,
"slug": "sport-motorcycle",
"title": "Sport Motorcycle",
"description": "",
"parent": 0,
"post_count": 2
}
],
"author": {
"id": 1,
"first_name": "",
},
"comments": [],
"attachments": [
{
"id": 17,
"url": "image url",
"slug": "yamaha-fz1",
"title": "2014 Yamaha FZ1 ",
"description": "",
"caption": "",
"parent": 16,
"mime_type": "image/jpeg",
"images": {
"full": {
"url": "http://demo..jpg",
"width": 640,
"height": 426
},
"thumbnail": {
"url": "http://demo..jpg",
"width": 150,
"height": 150
},
"medium": {
"url": "http://demo..jpg",
"width": 300,
"height": 199
},
"large": {
"url": "http://demo..jpg",
"width": 640,
"height": 426
}
}
}
],
"comment_count": 0,
"comment_status": "open",
"thumbnail": "http://demo..jpg",
"custom_fields": {
"slide": [
"http://demo..jpg"
]
},
........
}
请帮帮忙,我很迷惑:(在ADVA谢谢NCE
我的代码:
JsonArrayRequest postRequest = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
PostModel pm = new PostModel();
JSONObject posts = obj.getJSONObject("posts");
pm.settitle(posts.getString("title"));
pm.settitlePlain(posts.getString("titleplain"));
JSONObject category = obj.getJSONObject("category");
postList.add(pm);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
解决方案:
- 用于检查您的JSON有效或不直接使用此link
- 检查对象或数组在这个link,所以你可以确定根。
这项研究最后,这是有效的代码:
JSONArray posts = response.getJSONArray("posts"); for (int i = 0; i < posts.length(); i++) { JSONObject obj = posts.getJSONObject(i); PostModel pm = new PostModel(); pm.setJudul(obj.getString("title")); pm.setIsi(obj.getString("content")); JSONArray categories = obj.getJSONArray("categories"); for (int k = 0; k < categories.length(); k++) { JSONObject obj1 = categories.getJSONObject(k); pm.setCategory(obj1.getString("title")); } JSONObject thumbnail = obj.getJSONObject("thumbnail_images"); for (int j = 0; j < thumbnail.length(); j++) { JSONObject medium = thumbnail.getJSONObject("medium"); pm.setThumbnail(medium.getString("url")); } postList.add(pm); }
DYA,所以你必须在解析JSON数据正确的问题呢? – 2014-11-04 08:51:42
@ Dya,你的json无效。 – 2014-11-04 08:52:42
首先检查您发布到服务器的变量是否包含右键值?其次,因为Json会失效。为了检查你的Json是否有效,只需使用这个链接http://jsonlint.com/ – Piyush 2014-11-04 08:53:09