SoFunction
Updated on 2025-03-08

Various implementation methods for java to read JSON files

During the development process, you sometimes encounter the need to read local .json files. You usually write Reader code to read it yourself, but the code written in this way is a bit cumbersome (need to close the stream, create StringBuilder objects, etc.).

Recently I have found several gadgets that can make the requirement code more concise.

Prepare

json file: F:\

{
	"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. Use FileReader to read json file

package ;

import ;
import ;
import ;

import .*;
import ;
import ;
import ;

/**
 * @Author halon
 * @create 2021/9/
 */
public class ReadLocalJsonFileDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("F:\\");
        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 = ()) != -1) {
            ((char) ch);
        }
        ();
        ();
        String jsonStr = ();
        ((jsonStr));
    }
}

Console output:

{"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. Use jackson API to read json files

package ;

import ;
import ;
import ;

import .*;
import ;
import ;
import ;

/**
 * @Author halon
 * @create 2021/9/
 */
public class ReadLocalJsonFileDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("F:\\");
        jacksonMethod(file);
    }
    private static void jacksonMethod(File file) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ((file, ));
    }

Console output:

{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. Use nio to read json file

package ;

import ;
import ;
import ;

import .*;
import ;
import ;
import ;

/**
 * @Author halon
 * @create 2021/9/
 */
public class ReadLocalJsonFileDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("F:\\");
        nioMethod(file);
    }

    private static void nioMethod(File file) throws IOException {
        String jsonString = new String(((())));
        ((jsonString));
    }

Console output:

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

To be continued. . . .

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.