我丢失了4个小时来为显示到listview的Android应用设置Object或Array,遗憾的是结果为null。 我想得到:
.......
{
"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"
]
},
........
}
请帮助,我很困惑:(提前致谢
我的代码:
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();
}
});
解决方案:
本研究的最后一篇是有效的代码:
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);
}
答案 0 :(得分:0)
我认为您应该使用JsonObjectRequest
而非JsonArrayRequest
,因为您收到的对象不是数组
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
答案 1 :(得分:0)
我认为你不需要外部循环,因为它是一个JSONObject并检查这个
JSONObject posts = obj.getJSONObject("posts");
到
JSONObject posts = obj.getJSONArray("posts");
for(int i=0;i<posts.length();i++){
JSONObject checkObj = posts.getJSONObject(i);
//and rest parse it as it is
}
&#13;
答案 2 :(得分:0)
我建议你使用Gson。
Gson是一个来自Google的图书馆,它使用反射来解析JSON字符串,并且很容易就像你的神奇了!因此,您唯一的工作应该是使用JSON的结构创建模型对象。
public class YourObject {
private String status;
private int count;
private int count_total;
private int pages;
private List<Post> posts;
}
public class Post {
private int id;
private String url;
private String title;
private String title_plain;
private String content;
private List<Category> categories;
//... and so on
}
你不需要做任何其他事情。然后,解析只使用一行:
Gson gson = new Gson();
YourObject yourObject = gson.fromJson("yourJSONstring", YourObject.class);
这就是全部。你可以在这里找到Gson - &gt; https://code.google.com/p/google-gson/
答案 3 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
这里采用HashMap的ArrayList而不是自定义模型类:
JSONArray postsJsonArray = obj.getJSONArray("posts");
ArrayList<HashMap<String,Object>> postList = new ArrayList<HashMap<String, Object>>();
for(int j=0;j<postsJsonArray.length();j++){
HashMap<String,Object> post = new HashMap<String, Object>();
post.put("id",postsJsonArray.getJSONObject(j).getString("id"));
post.put("url",postsJsonArray.getJSONObject(j).getString("url"));
post.put("title",postsJsonArray.getJSONObject(j).getString("title"));
post.put("content",postsJsonArray.getJSONObject(j).getString("content"));
ArrayList<HashMap<String,String>> categoryList = new ArrayList<HashMap<String, String>>();
JSONArray categoriesJsonArray = postsJsonArray.getJSONObject(j).getJSONArray("categories");
for(int k=0;k<categoriesJsonArray.length();k++) {
HashMap<String, String> category = new HashMap<String, String>();
category.put("id", categoriesJsonArray.getJSONObject(j).getString("id"));
category.put("title", categoriesJsonArray.getJSONObject(j).getString("title"));
categoryList.add(category);
}
ArrayList<HashMap<String,String>> attachmentList = new ArrayList<HashMap<String, String>>();
JSONArray attachmentJsonArray = postsJsonArray.getJSONObject(j).getJSONArray("categories");
for(int l=0;l<attachmentJsonArray.length();l++) {
HashMap<String, String> attachment = new HashMap<String, String>();
attachment.put("id", attachmentJsonArray.getJSONObject(j).getJSONObject("image").getJSONObject("full").getString("url"));
attachmentList.add(attachment);
}
post.put("attachment",attachmentList);
postList.add(post);
}
答案 4 :(得分:0)
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonobj) {
try {
for(int i=0;i<jsonobj.getJSONArray("posts").length();i++){
String id=jsonobj.getJSONArray("posts").getJSONObject(i).getString("id");
for(int j=0;j<jsonobj.getJSONArray("posts").getJSONObject(i).getJSONArray("categories").length();j++){
String title=jsonobj.getJSONArray("posts").getJSONObject(i).getJSONArray("categories").getJSONObject(j).getString("title");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});