2016-04-15 55 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方法项类。