2014-11-04 118 views
-1

我失去了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(); 
      } 
     }); 

解决方案:

  1. 用于检查您的JSON有效或不直接使用此link
  2. 检查对象或数组在这个link,所以你可以确定根。
  3. 这项研究最后,这是有效的代码:

       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

DYA,所以你必须在解析JSON数据正确的问题呢? – 2014-11-04 08:51:42

+1

@ Dya,你的json无效。 – 2014-11-04 08:52:42

+0

首先检查您发布到服务器的变量是否包含右键值?其次,因为Json会失效。为了检查你的Json是否有效,只需使用这个链接http://jsonlint.com/ – Piyush 2014-11-04 08:53:09

回答

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) { 

       } 
      }); 
+0

但它的Json首先是无效的。这应该是评论或在这里添加一些代码的一部分! – Piyush 2014-11-04 08:56:18

+0

我试过使用JsonObjectRequest,只为第一个对象使用字符串:JSONObject phone = response.getJSONObject(“posts”);并且接下来的结果为空 – Dya 2014-11-04 09:12:04

+0

@Dya你能提供它的URL么? – Piyush 2014-11-04 09:14:52

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 
 
            }

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 - >https://code.google.com/p/google-gson/

+0

感谢您的建议,但如果我切换到其他代码。一切都会变得复杂,对吧?...我会在解决这个问题之后尝试。 – Dya 2014-11-04 09:19:06

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); 
} 
+0

我会尽力而为,等等..我会让你知道结果。 :)谢谢 – Dya 2014-11-04 09:36:35

+0

我必须把代码放在哪里?如果没有自定义模型类?如何将适配器设置为listview?对不起,如果我打扰你 – Dya 2014-11-04 09:47:35

+0

@ Dya,你的意思是不知道如何从ArrayList HashMap访问数据? – 2014-11-04 09:49:34

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) { 

         } 
        });