我失去了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