2014-11-04 123 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) { 

         } 
        });