2016-04-15 56 views
-2

首先,我想檢索嵌套在評級名稱中的所有費率值,並假設我不知道在那裏有多少評級,所以應該有某種類型的循環檢查它們。 json對象如下所示。ANDROID JSON從嵌套鍵檢索多個值

{ 
  "restaurant": [ 
    { 
      "restaurantId": "1", 
      "restaurantName": "Restaurant A", 
      "area": "Skudai", 
      "state": "Johor", 
      "rating": [ 
        { 
          "ratingId": "1", 
          "rate": "5", 
          "comment": null, 
          "userId": "15" 
        }, 
        { 
          "ratingId": "2", 
          "rate": "4", 
          "comment": null, 
          "userId": "14" 
        } 
      ] 
    }, 
    { 
      "restaurantId": "2", 
      "restaurantName": "Restaurant 2", 
      "area": "Skudai", 
      "state": "Johor", 
      "rating": null 
    } 
  ], 
  "success": 1 
} 

下面是在while循環的retrieveng部分看起來不太正確的代碼。對於getAverage中的JSONObject參數,我已經拿到了第一家只有餐廳A的餐廳,餐廳2不包括在內。

public int getAverage(JSONObject jsonObject){ 
    int average = 0, count = 0; 

    while (jsonObject.has("rate")){ 
     average += Integer.parseInt(jsonObject.optString("rate")); 
     count++; 
    } 

    average /= count; 

    return average; 
} 

如何使用optString()來計算平均從嵌套等級的檢索頻率的倍數值,或者我應該用其他的方式來找回?

+1

你的代碼在哪裏?你嘗試過什麼? –

+0

複製json響應,粘貼到http://json2csharp.com,它給你類作爲C#,將類轉換爲Java,使用GSON解析JSON。如果你不能,請檢查這個http://stackoverflow.com/questions/36625488/how-to-deserialize-json-with-a-string-url-name-value-pair/36625899#36625899 –

回答

1

如果你不想使用圖書館,你可以解析JSON字符串來獲取所有「評級」每個餐廳。

例子:

try{ 
     JSONObject o = new JSONObject(json); 
     JSONArray restaurants = o.getJSONArray("restaurant"); 
     for(int i = 0; i < restaurants.length(); i++){ 
      JSONObject restaurant = restaurants.getJSONObject(i); 
      JSONArray ratings = restaurant.getJSONArray("rating"); 
      for(int j = 0; j < ratings.length(); j++){ 
       JSONObject rating = ratings.getJSONObject(j); 
       String ratingId = rating.getString("ratingId"); 
       Log.d("TestJson ratingId", ratingId); 
       String rate = rating.optString("rate"); 
       Log.d("TestJson rate", rate); 
      } 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
+0

在這裏,我們有隻有ratingId和rate的單個值。如果我們需要所有的ID和速率然後? –

+0

您只需創建一個與餐廳相關的集合,並在for循環中將每個值添加到集合中... – Nirekin

+0

集合就像一個類中的getter setter方法\ –

0

谷歌已經制作了圖書館。

你不需要編寫循環和全部。更好的是你應該使用Java Class(Model/POJO)序列化/反序列化庫gson 它會自動將您的類轉換爲JSON/JSONArray

請不要練習寫很長的for循環。我有很多次只用於使用gson解析數組。

幫助,如果你的使用的Android工作室GSON with studio

2

您可以使用此代碼引用:

   ArrayList<DrawerCategory> list = new ArrayList<DrawerCategory>(); 
       ArrayList<DrawerSubCategory> ch_list; 
       JSONArray jArray = root.getJSONArray("restaurant"); 
       for (int i = 0; i < jArray.length(); i++) { 
        JSONObject jObject = jArray.getJSONObject(i); 
        DrawerCategory gru = new DrawerCategory(); 
        gru.setrestaurantId(jObject.getString("restaurantId")); 
        . 
        . 
        if (jObject.getString("rating").trim().equalsIgnoreCase("null")) { 
         ch_list = new ArrayList<DrawerSubCategory>(); 
         DrawerSubCategory ch = new DrawerSubCategory(); 
         ch.setName(jObject.getString("rating")); 
         ch_list.add(ch); 
         gru.setItems(ch_list); 
        } else { 
        JSONArray jArray1 = jObject.getJSONArray("rating"); 
         ch_list = new ArrayList<DrawerSubCategory>(); 
         for (int j = 0; j < jArray1.length(); j++) { 
          JSONObject jObject1 = jArray1.getJSONObject(j); 
          DrawerSubCategory ch = new DrawerSubCategory(); 
          ch.setratingId(jObject1.getString("ratingId")); 
          . 
          . 
          ch_list.add(ch); 
          gru.setItems(ch_list); 
         } 
        } 
        list.add(gru); 
       } 

這裏,DrawerCategory,DrawerSubCategory是包含getter和setter方法項類。