2015-05-23 64 views
0

我的問題是我的android應用程序不解析以下JSON數據。如果我使用其他json來源,它解析好。我已經驗證了json波紋管,並且沒有問題。它不會拋出任何錯誤。我不明白爲什麼。難道是因爲約會的第一天?android volley json解析器問題

JSON數據是這樣的:

{ 
    "2015-05-23 18:48:58": { 
    "Titlu": "kgjsdfklgjfdgjsdlgjdgjfd", 
    "PozaPrincipala": "27602", 
    "Descriere": "fkdsgjslkfdglkgfdsgfdklnm", 
    "CMSdate": "2015-05-23 18:48:58", 
    "url": "http://fsdgdgfdggsdfgfgfdg", 
    "thumb": "http://dasidsaionofafnoinfasnisa" 
    }, 
    "2015-05-21 20:17:36": { 
    "Titlu": "jhsdkgjshfgsjdfkhgsf", 
    "PozaPrincipala": "27592", 
    "Descriere": "kldsjgfhgdhgfhgsdfhifhgisf", 
    "CMSdate": "2015-05-21 20:17:36", 
    "url": "http://gsfdgfsdgsfdgfdgfdg", 
    "thumb": "http://dvsddggsfngfsgsfn" 
    } 
} 

而且我的解析代碼:

 private static final String url = "http://xxx.ro/xxx"; 
// Creating volley request obj 
     JsonArrayRequest movieReq = new JsonArrayRequest(url, 
       new Response.Listener<JSONArray>() { 

        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 
         hidePDialog(); 
         Log.d(TAG, response.toString()); 
         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 

           JSONObject obj = response.getJSONObject(i); 
           Movie movie = new Movie(); 
           movie.setTitle(obj.getString("Titlu")); 
           movie.setThumbnailUrl(obj.getString("thumb")); 

           movie.setLink(obj.getString("url")); 
           // adding movie to movies array 
           movieList.add(movie); 

          } catch (JSONException e) { 
           e.printStackTrace(); 

          } 

         } 

         // notifying list adapter about data changes 
         // so that it renders the list view with updated data 
         adapter.notifyDataSetChanged(); 
        } 
       }, new Response.ErrorListener() { 

      public void onErrorResponse(VolleyError error) { 

       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 


      } 
     }); 

回答

1

您的代碼不工作的原因是,即

{ 
    "2015-05-23 18:48:58": { 
     "Titlu": "This is the title", 
     "Descriere": "Description", 
     "CMStags": "tags", 
     "CMSdate": "2015-05-23 18:48:58", 
     "url": "http:\/\/www.xxx.ro/jdshafhdafhkas", 
     "thumb": "http:\/\/img.xxx.ro\/?u=http%3A%2F%2Fst.xxx.ro%2Fcms_websites%2Fcms_something%2Flibrary%2Fimg%2F2015%2F05%2Fsome_thumb.jpg&amp;w=300&amp;h=215&c=1" 
    } 
} 

你的反應是有一個JSONObject"2015-05-23 18:48:58"

你叫

JSONObject obj = response.getJSONObject(i); 

後再次提出另一個JSONObject

JSONObject objItem = obj.getJSONObject("2015-05-23 18:48:58"); 

現在做你的事

Movie movie = new Movie(); 
movie.setTitle(objItem.getString("Titlu")); 
... 
... 

此外,爲了以防萬一,你必須拿到鑰匙值(如"2015-05-23 18:48:58" ..如你所說有30個)閱讀java-iterate-over-jsonobject

類似下面的代碼就可以解決問題

jObject = new JSONObject(contents.trim()); 
Iterator<?> keys = jObject.keys(); 

while(keys.hasNext()) { 
    String key = (String)keys.next(); 
    // save this key in a ArrayList of String for 
    // passing to JSONObject objItem = obj.getJSONObject(key); 
    // instead of JSONObject objItem = obj.getJSONObject("2015-05-23 18:48:58"); 
    if (jObject.get(key) instanceof JSONObject) { 

    } 
} 
+0

這僅僅是第一數據,json的有近30項 – gogu

+0

好期待,我將編輯 –

+0

我已編輯ans,我希望它有幫助 –

0

此JSON數據是錯誤的! 正確的是:

{ 
    "2015-05-23 18:48:58": { 
    "Titlu": "This is the title", 
    "Descriere": "Description", 
    "CMStags": "tags", 
    "CMSdate": "2015-05-23 18:48:58", 
    "url": "http:\/\/www.xxx.ro/jdshafhdafhkas", 
    "thumb": "http:\/\/img.xxx.ro\/?u=http%3A%2F%2Fst.xxx.ro%2Fcms_websites%2Fcms_something%2Flibrary%2Fimg%2F2015%2F05%2Fsome_thumb.jpg&amp;w=300&amp;h=215&c=1" 
    } // you need to end-up the object 
} 

在這種情況下,以正確的方式來獲取數據是:

JSONObject parentObj = response.getJSONObject(i); 
JSONObject obj= parentObj .getJSONObject("2015-05-23 18:48:58"); 
0

不能讓它在與迭代凌空工作,所以我要把這個解決方案,但它給了我無法解析法鍵設置():

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      for (String key : response.keySet()) { 

         try { 
          JSONObject obj = response.getJSONObject(key); 
          Log.d(TAG, obj.toString()); 


          Movie movie = new Movie(); 
          movie.setTitle(obj.getString("Titlu")); 
          movie.setThumbnailUrl(obj.getString("thumb")); 

          movie.setLink(obj.getString("url")); 
          // adding movie to movies array 
          movieList.add(movie); 

         } catch (JSONException e) { 
          e.printStackTrace(); 

         } 

        } 

        // notifying list adapter about data changes 
        // so that it renders the list view with updated data 
        adapter.notifyDataSetChanged(); 
       } 
      }