简体   繁体   English

Java从JSON文件解析JSON数组

[英]Java parse JSON array from JSON file

I haven't been programming for a long time but I like it and trying to get back on track. 我已经很长时间没有编程了,但是我喜欢它并试图回到正轨。 So please excuse the nature of the problem/question here. 因此,请在这里原谅问题的性质。

What I need is pretty simple in my opinion but i'm mostly struggling with using gson and json-simple to read my json file, one at a time, and be able to retrieve the values. 我认为我需要的非常简单,但是我一直在努力使用gson和json-simple一次读取一个json文件,并且能够检索值。

I have seen many approaches on here but as I said been a while and I have not done Java a lot in my career. 我在这里看到了许多方法,但是正如我所说的,已经有一段时间了,在我的职业生涯中我并没有做很多Java。 So need some guidance/explanation on the best approach. 因此,需要有关最佳方法的一些指导/说明。

JSON: JSON:

[{ "car": "Toyota", "colour": "red", "qty": "1","date_manufactured":"12972632260006" }, { "car": "Hyundai", "colour": "red", "qty": "2","date_manufactured":"1360421626000" }, { "car": "Kia", "colour": "blue", "qty": "2", "date_manufactured":"1265727226000"}, ]

Any help to put me on the right track is appreciated! 任何帮助我走上正轨的帮助都将受到赞赏!

  1. Create a POJO class to represent your JSON data: 创建一个POJO类来表示您的JSON数据:

     public class CarInfo { String car; String colour; String qty; String date_manufactured; } 
  2. Use GSON to parse JSON String Array 使用GSON解析JSON字符串数组

     String carInfoJson = "[{ \\"car\\": \\"Toyota\\", \\"colour\\": \\"red\\",\\"qty\\": \\"1\\",\\"date_manufactured\\":\\"12972632260006\\" }, { \\"car\\":\\"Hyundai\\", \\"colour\\":\\"red\\",\\"qty\\":\\"2\\",\\"date_manufactured\\":\\"1360421626000\\" }]"; Gson gson = new Gson(); CarInfo[] carInfoArray = gson.fromJson(carInfoJson, CarInfo[].class); 
  3. Use GSON to parse JSON String Array from a file 使用GSON解析文件中的JSON字符串数组

     String carInfoJson= new String(Files.readAllBytes(Paths.get("filename.txt"))); Gson gson = new Gson(); CarInfo[] carInfoArray = gson.fromJson(carInfoJson, CarInfo[].class); 
  4. Use GSON to parse JSON String Array from a file using BufferedReader 使用GSON通过BufferedReader从文件解析JSON字符串数组

     BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); Gson gson = new Gson(); CarInfo[] carInfoArray = gson.fromJson(reader, CarInfo[].class); } catch (FileNotFoundException ex) { ... } finally { ... } 
  5. Use GSON to parse JSON String Array from a file using JsonReader in stream mode 使用GSON在流模式下使用JsonReader从文件解析JSON字符串数组

     try { InputStream stream = new FileInputStream("c:\\\\filename.txt"); JsonReader reader = new JsonReader(new InputStreamReader(stream, "UTF-8")); Gson gson = new Gson(); // Read file in stream mode reader.beginArray(); while (reader.hasNext()) { CarInfo carInfo = gson.fromJson(reader, CarInfo.class); } reader.endArray(); reader.close(); } catch (UnsupportedEncodingException ex) { ... } catch (IOException ex) { ... } 

I'm using json-simple to explain how to do this. 我正在使用json-simple来说明如何执行此操作。 Your json is a JSONArray (because it starts and ends with square brackets) with JSONObject (curly brackets with pair list) inside so first you've to extract the array using a JSONParser and then you can easily iterate over it and get fields from each JSONObject . 您的json是一个JSONArray (因为它以方括号开头和结尾),内部带有JSONObject (带有成对列表的花括号),因此首先您必须使用JSONParser提取数组,然后可以轻松地对其进行迭代并从每个数组中获取字段JSONObject Here is a simple example it just shows you an easy and understandable way: 这是一个简单的示例,它只是向您显示了一种简单易懂的方式:

String json = "[{ \"car\": \"Toyota\", \"colour\": \"red\", \"qty\": \"1\",\"date_manufactured\":\"12972632260006\" }, { \"car\": \"Hyundai\", \"colour\": \"red\", \"qty\": \"2\",\"date_manufactured\":\"1360421626000\" }]";
JSONParser parser = new JSONParser();
try {
    /* It's a JSONArray first. */
    JSONArray tmpArr = (JSONArray)parser.parse(json);
    for(Object obj : tmpArr){
        /* Extract each JSONObject */
        JSONObject tmpObj = (JSONObject) obj;
        System.out.println(tmpObj.get("car"));
        System.out.println(tmpObj.get("colour"));
        System.out.println(tmpObj.get("qty"));
        System.out.println(tmpObj.get("date_manufactured"));
    }
} catch (ParseException e) {
    e.printStackTrace();
}

Note that you can use Gson, it's much more complete then json-simple but a little bit trickier. 请注意,您可以使用Gson,它比json-simple更为完整,但有点棘手。

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

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