当前位置: 代码网 > it编程>编程语言>Java > java读取JSON文件的多种实现方式

java读取JSON文件的多种实现方式

2024年06月10日 Java 我要评论
在开发过程中有时会遇到需要读取本地.json文件的需求,通常会自己写reader代码去读,但是这么做写出来的代码有些繁琐(需要关流、创建stringbuilder对象等操作)。最近发现几个小工具可以让

在开发过程中有时会遇到需要读取本地.json文件的需求,通常会自己写reader代码去读,但是这么做写出来的代码有些繁琐(需要关流、创建stringbuilder对象等操作)。

最近发现几个小工具可以让需求代码变得更加简洁。

准备

json文件:f:\halon.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文件

package com.tool;

import com.alibaba.fastjson.json;
import com.alibaba.fastjson.jsonobject;
import com.fasterxml.jackson.databind.objectmapper;

import java.io.*;
import java.nio.file.files;
import java.nio.file.paths;
import java.util.map;

/**
 * @author halon
 * @create 2021/9/
 */
public class readlocaljsonfiledemo {
    public static void main(string[] args) throws ioexception {
        file file = new file("f:\\halon.json");
        readermethod(file);

    }
    private static void readermethod(file file) throws ioexception {
        filereader filereader = new filereader(file);
        reader reader = 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();
        string jsonstr = sb.tostring();
        system.out.println(json.parseobject(jsonstr));
    }
}

控制台输出:

{"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文件

package com.tool;

import com.alibaba.fastjson.json;
import com.alibaba.fastjson.jsonobject;
import com.fasterxml.jackson.databind.objectmapper;

import java.io.*;
import java.nio.file.files;
import java.nio.file.paths;
import java.util.map;

/**
 * @author halon
 * @create 2021/9/
 */
public class readlocaljsonfiledemo {
    public static void main(string[] args) throws ioexception {
        file file = new file("f:\\halon.json");
        jacksonmethod(file);
    }
    private static void jacksonmethod(file file) throws ioexception {
        objectmapper objectmapper = new objectmapper();
        system.out.println(objectmapper.readvalue(file, map.class));
    }

控制台输出:

{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文件

package com.tool;

import com.alibaba.fastjson.json;
import com.alibaba.fastjson.jsonobject;
import com.fasterxml.jackson.databind.objectmapper;

import java.io.*;
import java.nio.file.files;
import java.nio.file.paths;
import java.util.map;

/**
 * @author halon
 * @create 2021/9/
 */
public class readlocaljsonfiledemo {
    public static void main(string[] args) throws ioexception {
        file file = new file("f:\\halon.json");
        niomethod(file);
    }

    private static void niomethod(file file) throws ioexception {
        string jsonstring = new string(files.readallbytes(paths.get(file.getpath())));
        system.out.println(jsonobject.parseobject(jsonstring));
    }

控制台输出:

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

未完待续。。。。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com