【问题标题】:How to iterrate through Json Array and return each value如何遍历Json Array并返回每个值
【发布时间】:2021-07-05 09:03:05
【问题描述】:

我有一个包含 Json 对象数组的 Json 文件:

[{"id":"939f0080-e93e-4245-80d3-3ac58a4a4335","name":"Micha","date":"2021-04-20T11:21:48.000Z","entry":"Wow"}, {"id":"939f0070-e93f-4235-80d3-3ac58a4a4324","name":"Sarah","date":"2021-04-21T11:21:48.000Z","entry":"Hi"}, {"id":"897f0080-e93e-4235-80d3-3ac58a4a4324","name":"John","date":"2021-04-25T17:11:48.000Z","entry":"Hi how are you"}...]

我正在使用 Json-simple 来获取数组,但我只能获取对象,而不能获取值。

JSONParser jsonParser = new JSONParser();

        try {
            FileReader reader = new FileReader("j.json");
            Object object = jsonParser.parse(reader);
            JSONArray jsonArray = (JSONArray) object;
            //prints the first Object
            System.out.println("element 1 is" +  jsonArray.get(0));
            
            //prints whole Array
            System.out.println(jsonArray);

我如何遍历我的文件并获取每个日期、名称日期和条目的值而不是对象?

我想得到类似的东西:

"id is 939f0080-e93e-4245-80d3-3ac58a4a4335 name is Micha date is 2021-04-20T11:21:48.000Z enry is wow"
"id is 939f0070-e93f-4235-80d3-3ac58a4a4324 name is Sarah 2021-04-21T11:21:48.000Z date is 2021-04-21T11:21:48.000Z"
"name is ..."

【问题讨论】:

    标签: java json json-simple


    【解决方案1】:

    你想要的基本上就是这个

        public static void main(String[] args) {
            JSONParser jsonParser = new JSONParser();
    
            try (FileReader reader = new FileReader("j.json")) {
                Object object = jsonParser.parse(reader);
                JSONArray jsonArray = (JSONArray) object;
                for (Object o : jsonArray) {
                    JSONObject jsonObject = (JSONObject) o;
                    System.out.printf("id is %s name is %s date is %s entry is %s%n", jsonObject.get("id"), jsonObject.get("name"), jsonObject.get("date"), jsonObject.get("entry"));
    
                    // Or if you want all
                    for (Object key : jsonObject.keySet()) {
                        System.out.printf("%s is %s", key, jsonObject.get(key));
                    }
                    System.out.println();
                }
            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }
        }
    

    如果属性是可选的,您可以使用getOrDefault。还有无数其他库可以将您的 json 转换为 java 对象。这将为您提供更多的类型安全性。例如。 jacksongson

    【讨论】:

      【解决方案2】:

      JSONArray 实现了 Collection 和 Iterable,因此您可以使用 For 循环或使用 Iterator 或 Stream 对其进行迭代。遗憾的是,该对象不是泛型类型的,因此您总是会得到 Objects 并且必须自己转换它们:

      for(Object value : jsonArray) {
         // your code here //
      }
      

      【讨论】:

        【解决方案3】:

        希望这会有所帮助:

        JSONParser jsonParser = new JSONParser();
        
        try {
            Object object = jsonParser.parse("[{\"id\":\"939f0080-e93e-4245-80d3-3ac58a4a4335\",\"name\":\"Micha\",\"date\":\"2021-04-20T11:21:48.000Z\",\"entry\":\"Wow\"}, {\"id\":\"939f0070-e93f-4235-80d3-3ac58a4a4324\",\"name\":\"Sarah\",\"date\":\"2021-04-21T11:21:48.000Z\",\"entry\":\"Hi\"}, {\"id\":\"897f0080-e93e-4235-80d3-3ac58a4a4324\",\"name\":\"John\",\"date\":\"2021-04-25T17:11:48.000Z\",\"entry\":\"Hi how are you\"}]");
            JSONArray jsonArray = (JSONArray) object;
        
            jsonArray.forEach(x -> {
                JSONObject o = (JSONObject) x;
                String collect = o.entrySet()
                        .stream()
                        .map(e -> e.getKey() + " is " + e.getValue().toString())
                        .collect(Collectors.joining(" "));
                System.out.println(collect);
            });
        } catch (ParseException e) {
            e.printStackTrace();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-18
          • 2019-02-03
          • 2013-08-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多