2016-12-14 32 views
0

我有一個像下面這樣的JSON結構,我無法到達圖像節點。我如何用Volley庫做到這一點?任何想法都會有所幫助。提前致謝。我怎樣才能到達圖像節點

{ 
    "nodes": [ 
    { 
     "node": { 
     "id": "2536", 
     "title": "Die Düsseldorfer Rabbiner", 
     "image": { 
      "src": "how to reach here", 
      "alt": "", 
      "title": "© Landeshauptstadt Düsseldorf/Michael Gstettenbauer" 
     }, 
     "body": "some text", 
     "category": "Deutsch", 
     "created": "1481344134", 
     "url": "some text" 
     } 
    } 
    ] 
} 

代碼塊

JsonArrayRequest getRequest = new JsonArrayRequest(
      url, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        try { 
         JSONObject jsonObject = new JSONObject("response"); 
         JSONArray jsonArray = jsonObject.getJSONArray("nodes"); 
         for(int i = 0; i<jsonArray.length(); i++){ 
          JSONObject jo = jsonArray .getJSONObject(i); 
          String name = jo.getString("category"); 

          Toast.makeText(context, name, Toast.LENGTH_LONG).show(); 

         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
+0

你如何解析你的json,手動或使用任何類似GSON的庫。 –

+0

'新的JSONObject(「對象」)'你認爲那是什麼? – njzk2

+0

嗨Ravinder,我已經添加了我的代碼塊。你可以檢查一下嗎?我認爲我做對了,但它不起作用。 – nuhkoca

回答

1
From the look of things you should use JsonObjectRequest instead JsonArrayRequest. 

JsonObjectRequest request=new JsonObjectRequest(url, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 

        try { 
         JSONArray array = response.getJSONArray("nodes"); 
         for(int i=0;i<array.length();i++){ 
          JSONObject object= array.getJSONObject(i); 
          JSONObject imageObject= object.getJSONObject("node").getJSONObject("image"); 
          Toast.makeText(TestActivity.this, imageObject.toString(), Toast.LENGTH_LONG).show(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      }); 
+0

嘿,那對我有效。謝謝! – nuhkoca

1

你的Java模型應該匹配JSON 準確。 JSON模式中的基本元素是對象,而不是數組;因此,你的要求應該是一個JSONObjectRequest,而不是一個JSONArrayRequest

JSONObjectRequest request = new JSONObjectRequest(url, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        try { 
         JSONArray nodes = response.getJSONArray("nodes"); 
         for(int i = 0; i < nodes.length; i++) { 
          JSONObject obj = nodes.getJSONObject(i); 
          JSONObject node = obj.getJSONObject("node"); 
          JSONObject image = node.getJSONObject("image"); 
          String title = image.getString("title"); 

          Toast.makeText(context, title, Toast.LENGTH_LONG).show(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 

正如你可以看到,這可能會很麻煩,如果你有一個大的數據集。這就是爲什麼我強烈推薦使用JSON解析庫;如GSON,MoshiJackson。有一個很好的教程,介紹如何使用Volley進行自定義請求,以便使用training documentation中的這些庫。

+0

嘿,那對我有效。你太棒了。謝謝! – nuhkoca