2017-08-12 24 views
-1

嗨,我想穿過這個JSON數據使用android快速聯網庫,但我不能這樣做。有人可以請建議一種方式來獲取數組中的所有對象的ID和文件名?如何遍歷java中的這個json數據?

注意 - 它必須採用Android快速的網絡庫來完成 - https://github.com/amitshekhariitbhu/Fast-Android-Networking

[ 
      { 
      "_id": "598ae8773376673353ef6da6", 
      "filename": "nldcjjzpesyhkkx1502275703450.jpg", 
      "votes": 33, 
      "__v": 0 
      }, 
      { 
      "_id": "598ae83b3376673353ef6da5", 
      "filename": "xlwkfcxicwhqibw1502275643486.jpg", 
      "votes": 31, 
      "__v": 0 
      }, 
      { 
      "_id": "598a2e7000c6717c3c04e534", 
      "filename": "ndkiqptbifqmnjz1502228078447.jpg", 
      "votes": 19, 
      "__v": 0 
      }, 
      { 
      "_id": "5989ece9673738c027377710", 
      "filename": "seicmgkzubzavfy1502211303478.jpg", 
      "votes": 13, 
      "__v": 0 
      } 
] 

回答

2

您可以使用GSON lib或可以檢索使用類似這樣的循環:

try { 
    JSONArray jsonArray = new JSONArray(yourResponseStrOrJson); 
    for(int i = 0; i < jsonArray.length(); i++){ 
     JSONObject jsonObject = jsonArray.getJSONObject(i); 
     Log.d("ID", jsonObject.getString("_id")); 
     //TOTO retrive other element 
    } 
}catch (JSONException e){ 
    e.printStackTrace(); 
} 
+0

沒有我需要使用android快速聯網庫 - 這裏是鏈接 https://github.com/amitshekhariitbhu/Fast-Android-Networking – knightcube

+0

@Knightcube,將上面的代碼放在onResponse方法中,並且您是否喜歡 –

+0

Ye現在工作啊。其實我有一些其他連接問題,這就是爲什麼它不工作。我的壞 – knightcube