广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >java— 读取JSON文件的多种方式
  • 628
分享到

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

javajsonPoweredby金山文档 2023-09-01 08:09:31 628人浏览 薄情痞子
摘要

大部分内容参考自: https://blog.csdn.net/csdn_halon/article/details/120287992 在开发过程中有时会遇到需要读取本地.JSON文件的需求,通常会自己写Reader代码去读,但是

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

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

准备:

json文件:D:\test.json

{    "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    }}

1.使用FileReader读取json文件

1.1、添加依赖

                       com.alibaba           fastjson           2.0.12            

1.2源代码

import com.alibaba.fastjson.JSON; import java.io.*; public class ReadLocalJsonFileDemo {    publicstatic void main(String[] args) throws IOException {        Filefile = new File("D:\\test.json");       readerMethod(file);     }     privatestatic void readerMethod(File file) throws IOException {       FileReader fileReader = new FileReader(file);        Readerreader = new InputStreamReader(new FileInputStream(file), "Utf-8");        int ch= 0;       StringBuffer sb = new StringBuffer();        while((ch = reader.read()) != -1) {           sb.append((char) ch);        }       fileReader.close();       reader.close();        StringjsonStr = sb.toString();       System.out.println(JSON.parseObject(jsonStr));    }}

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、添加依赖

                       com.fasterxml.jackson.core           jackson-databind           2.13.3            

2.2源代码

import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File;import java.io.IOException;import java.util.Map; public class ReadLocalJsonFileDemo {    publicstatic void main(String[] args) throws IOException {        Filefile = new File("D:\\test.json");       jacksonMethod(file);    }     privatestatic void jacksonMethod(File file) throws IOException {       ObjectMapper objectMapper = new ObjectMapper();       System.out.println(objectMapper.readValue(file, Map.class));    }}

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、添加依赖

                       com.alibaba           fastjson           2.0.12            

3.2源代码

import com.alibaba.fastjson.JSONObject; import java.io.File;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Paths; public class ReadLocalJsonFileDemo {    publicstatic void main(String[] args) throws IOException {        Filefile = new File("D:\\test.json");       nioMethod(file);    }     privatestatic void nioMethod(File file) throws IOException {        StringjsonString = new String(Files.readAllBytes(Paths.get(file.getPath())));       System.out.println(JSONObject.parseObject(jsonString));    }}

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}

来源地址:https://blog.csdn.net/xijinno1/article/details/129172727

--结束END--

本文标题: java— 读取JSON文件的多种方式

本文链接: https://www.lsjlt.com/news/387927.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作