当前位置:   article > 正文

java— 读取JSON文件的多种方式_java读取json文件

java读取json文件

大部分内容参考自: https://blog.csdn.net/csdn_halon/article/details/120287992

在开发过程中有时会遇到需要读取本地.json文件的需求,通常会自己写Reader代码去读,但是这么做写出来的代码有些繁琐(需要关流、创建StringBuilder对象等操作)。最近发现几个小工具可以让需求代码变得更加简洁。

准备:

json文件:D:\test.json

  1. {
  2. "ID": 10001,
  3. "detail": "detail",
  4. "json_format_version": 1.0,
  5. "other_info": {
  6. "array_one": [
  7. [855, 410],
  8. [854, 411],
  9. [847, 411],
  10. [846, 410],
  11. [845, 410],
  12. [844, 409]
  13. ],
  14. "array_two": [
  15. [832, 303],
  16. [829, 303],
  17. [828, 302],
  18. [825, 302],
  19. [824, 301]
  20. ],
  21. "array_three": [
  22. [1013, 224],
  23. [1012, 225],
  24. [1010, 225],
  25. [1009, 226],
  26. [1023, 224]
  27. ],
  28. "point": [853, 310],
  29. "boolean": true
  30. }
  31. }

1.使用FileReader读取json文件

1.1、添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>2.0.12</version>
  6. </dependency>
  7. </dependencies>

1.2源代码

  1. import com.alibaba.fastjson.JSON;
  2. import java.io.*;
  3. public class ReadLocalJsonFileDemo {
  4. publicstatic void main(String[] args) throws IOException {
  5. Filefile = new File("D:\\test.json");
  6. readerMethod(file);
  7. }
  8. privatestatic void readerMethod(File file) throws IOException {
  9. FileReader fileReader = new FileReader(file);
  10. Readerreader = new InputStreamReader(new FileInputStream(file), "Utf-8");
  11. int ch= 0;
  12. StringBuffer sb = new StringBuffer();
  13. while((ch = reader.read()) != -1) {
  14. sb.append((char) ch);
  15. }
  16. fileReader.close();
  17. reader.close();
  18. StringjsonStr = sb.toString();
  19. System.out.println(JSON.parseObject(jsonStr));
  20. }
  21. }

1.3.控制台输出

{"other_info":{"array_two":[[832,303],[829,303],[828,302],[825,302],[824,301]],"array_three":[[1013,224],[1012,225],[1010,225],[1009,226],[1023,224]],"boolean":true,"array_one":[[855,410],[854,411],[847,411],[846,410],[845,410],[844,409]],"point":[853,310]},"ID":10001,"detail":"detail","json_format_version":1.0}

2.使用jacksonAPI读取json文件

2.1、添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.fasterxml.jackson.core</groupId>
  4. <artifactId>jackson-databind</artifactId>
  5. <version>2.13.3</version>
  6. </dependency>
  7. </dependencies>

2.2源代码

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Map;
  5. public class ReadLocalJsonFileDemo {
  6. publicstatic void main(String[] args) throws IOException {
  7. Filefile = new File("D:\\test.json");
  8. jacksonMethod(file);
  9. }
  10. privatestatic void jacksonMethod(File file) throws IOException {
  11. ObjectMapper objectMapper = new ObjectMapper();
  12. System.out.println(objectMapper.readValue(file, Map.class));
  13. }
  14. }

2.3.控制台输出

{ID=10001,detail=detail, json_format_version=1.0, other_info={array_one=[[855, 410],[854, 411], [847, 411], [846, 410], [845, 410], [844, 409]], array_two=[[832,303], [829, 303], [828, 302], [825, 302], [824, 301]], array_three=[[1013,224], [1012, 225], [1010, 225], [1009, 226], [1023, 224]], point=[853, 310],boolean=true}}

3.使用nio读取json文件

3.1、添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>2.0.12</version>
  6. </dependency>
  7. </dependencies>

3.2源代码

  1. import com.alibaba.fastjson.JSONObject;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.Paths;
  6. public class ReadLocalJsonFileDemo {
  7. publicstatic void main(String[] args) throws IOException {
  8. Filefile = new File("D:\\test.json");
  9. nioMethod(file);
  10. }
  11. privatestatic void nioMethod(File file) throws IOException {
  12. StringjsonString = new String(Files.readAllBytes(Paths.get(file.getPath())));
  13. System.out.println(JSONObject.parseObject(jsonString));
  14. }
  15. }

3.3.控制台输出

{"other_info":{"array_two":[[832,303],[829,303],[828,302],[825,302],[824,301]],"array_three":[[1013,224],[1012,225],[1010,225],[1009,226],[1023,224]],"boolean":true,"array_one":[[855,410],[854,411],[847,411],[846,410],[845,410],[844,409]],"point":[853,310]},"ID":10001,"detail":"detail","json_format_version":1.0}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/121137
推荐阅读
相关标签
  

闽ICP备14008679号