java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java读取JSON文件的方式

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

作者:csdn_halon

这篇文章主要介绍了java读取JSON文件的多种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

在开发过程中有时会遇到需要读取本地.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}

未完待续。。。。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文