简体   繁体   English

如何从 json 文件中读取所有 JSON 对象? 爪哇

[英]How to read all JSON objects from json file? Java

When I try to read objects from Json file - it only iterate through one object 4 times.当我尝试从 Json 文件中读取对象时 - 它只遍历一个对象 4 次。

public static void main(String[] args) throws Exception {

        String file = "src/main/resources/ip.json";
        String json = readFileAsString(file);
        JSONObject jo = new JSONObject(json);
       
        HashMap result = new ObjectMapper().readValue(json, HashMap.class);
        
            for (Object entry : result.keySet()) {

                String id = (String) jo.get().get("id");
                System.out.println(id);
                String ip = (String) jo.get("ip");
                System.out.println(ip);
                Integer score = (Integer) jo.get("score");
                System.out.println(score);

            }

        }

        public static String readFileAsString (String file) throws IOException {
            return new String(Files.readAllBytes(Paths.get(file)));
        }

    }

------------------------------------Json File-------------------------------- -------------------------------------Json 文件------------ --------------------

{
  "id": "test",
  "score": 12,
  "ip": "1.2.3.4",
  "message": "Hi"
},
{
"id": "test",
"score": 5,
"ip": "1.2.3.5"
},
{
"id": "test",
"score": 17,
"ip": "1.2.3.4"
},
{
"id": "test2",
"score": 9,
"ip": "1.2.3.4"
}

OUTPUT:输出:

test 1.2.3.4 12 test 1.2.3.4 12 test 1.2.3.4 12 test 1.2.3.4 12测试 1.2.3.4 12 测试 1.2.3.4 12 测试 1.2.3.4 12 测试 1.2.3.4 12

If you want to read MULTIPLE json objects, they should be in a JSON array.如果你想读取多个 json 对象,它们应该在一个 JSON 数组中。 For example,例如,

[{"id": "test", "score": 12, "ip": "1.2.3.4", "message": "Hi" }, { "id": "test", "score": 5, "ip": "1.2.3.5" }, { "id": "test", "score": 17, "ip": "1.2.3.4" }, { "id": "test2", "score": 9, "ip": "1.2.3.4" }]

Please refer to similar question here .请参考这里的类似问题。

Your Json file does not have a json array, only an objects.您的 Json 文件没有 json 数组,只有一个对象。 Array is something like this.数组是这样的。

[
 {
   "id": "test",
   "score": 12,
   "ip": "1.2.3.4",
   "message": "Hi"
 },
 {
  "id": "test",
  "score": 5,
  "ip": "1.2.3.5"
 },
 {
  "id": "test",
  "score": 17,
  "ip": "1.2.3.4"
 },
 {
  "id": "test2",
  "score": 9,
  "ip": "1.2.3.4"
 }
]

So edit your Json file like above and try this.所以像上面一样编辑你的 Json 文件并尝试这个。

public static void main(String[] args) throws Exception {

        String file = "src/main/resources/ip.json";
        String json = readFileAsString(file);
        JSONArray jsonArray = new JSONArray(json);
        for(int i=0; i < jsonArr.length();i++){
            JSONObject jo = jsonArr.getJSONObject(i);       
            HashMap result = new ObjectMapper().readValue(jo, HashMap.class);
        
            for (Object entry : result.keySet()) {

                String id = (String) jo.get().get("id");
                System.out.println(id);
                String ip = (String) jo.get("ip");
                System.out.println(ip);
                Integer score = (Integer) jo.get("score");
                System.out.println(score);

            }

        }

        public static String readFileAsString (String file) throws IOException {
            return new String(Files.readAllBytes(Paths.get(file)));
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM