【问题标题】:How to count json object item?如何计算json对象项?
【发布时间】:2019-10-21 05:58:10
【问题描述】:

我在 Android 中解析一个 JSON 字符串,如下所示:

 [
   {
     "id":70,
     "selection":"25"
    },
    {
      "id":71,
      "selection":"50"
    },
    {
      "id":72,
      "selection":"50"
    }
 ]

现在我想获取所有selection 的总数并将其显示在文本视图中。谁能给我一个如何做到这一点的例子,或者任何关于这个的教程?

例如:

选择 25 = 1
选择 50 = 2

感谢您的帮助!

【问题讨论】:

    标签: java android json android-volley


    【解决方案1】:

    我认为您正在寻找的是这样的:

            JsonArray selections = new JsonArray(); // This is your parsed json object
            HashMap<Integer, Integer> count = new HashMap<>();
            for (JsonElement element : selections) {
                JsonObject jsonObject = element.getAsJsonObject();
                if(jsonObject.has("selection")) {
                    int selValue = jsonObject.get("selection").getAsInt();
                    if(count.containsKey(selValue)) {
                        count.put(selValue, count.get(selValue) + 1);
                    } else {
                        count.put(selValue, 1);
                    }
                }
            }
    

    这将做的是遍历您的 json 数组并获取每个选择元素的值。为了跟踪计数,它会增加 count hashmap 内的计数。

    然后您可以从哈希图中获取特定值的计数:

    count.get(25); // returns 1
    count.get(50); // returns 2
    // etc...
    

    【讨论】:

    • 请记住,确切的实现可能会有所不同。对于这个实现,使用了谷歌 gson。根据确切的 json 库,可能需要稍微不同的方法。
    【解决方案2】:

    你可以得到json的数组并迭代计算选择的总和

    JSONArray selections = jsonObj.getJSONArray("selections");
    
    // looping through All Selections
    int totalCount = selections.length();
    
    

    【讨论】:

    • 这似乎不符合用户的要求。这计算了整体选择的总数,而问题似乎是指选择发生的总数。 (计数,而不是总和)
    • 对不起,是的,你是对的,我错过了理解问题
    【解决方案3】:

    如果您在 Java 8 中使用Jackson,您可以先将给定的 JSON 字符串转换为List&lt;Map&lt;String, Object&gt;&gt;,然后再将其转换为List&lt;Integer&gt; 以供选择。最后,您可以按如下方式计算此列表中的出现次数:

    ObjectMapper mapper = new ObjectMapper();
    List<Map<String, Object>> jsonObj = mapper.readValue(jsonStr, new TypeReference<List<Map<String, Object>>>(){});
    Map<Integer, Long> counted = jsonObj.stream()
            .map(x -> Integer.valueOf(x.get("selection").toString()))
            .collect(Collectors.toList())
            .stream()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    
    System.out.println(counted.toString());
    

    控制台输出:

    {50=2, 25=1}

    【讨论】:

      【解决方案4】:
          ArrayList<String> data = new ArrayList<>();
          ArrayList<String> datacount = new ArrayList<>();
      
      
                  jsonStr = "Your JSON"
      
                  JSONArray jsonArr= null;
                  try {
                      jsonArr = new JSONArray(jsonStr);
                      for (int i = 0; i < jsonArr.length(); i++) {
                          JSONObject jsonObj = jsonArr.getJSONObject(i);
                          //here you can set to TextView
                          String selection = jsonObj.getString("selection");
                          //System.out.println("adcac"+selection);
      
                          if (data.contains(selection)) {
      
                              int index = data.indexOf(selection);
      
                              int count = Integer.parseInt(datacount.get(index))+1;
      
          //                    System.out.println("Index==="+index+"---count---"+count);
                              datacount.set(index,String.valueOf(count));
                          } else {
                              datacount.add(String.valueOf(1));
                              data.add(selection);
                          }
           // Here you can get data and data count...  
          //                System.out.println("data---"+datacount.toString());
                      }
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 1970-01-01
        • 2016-01-10
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        相关资源
        最近更新 更多