大部分內(nèi)容參考自: https://blog.csdn.net/csdn_halon/article/details/120287992
在開發(fā)過程中有時會遇到需要讀取本地.json文件的需求,通常會自己寫Reader代碼去讀,但是這么做寫出來的代碼有些繁瑣(需要關(guān)流、創(chuàng)建StringBuilder對象等操作)。最近發(fā)現(xiàn)幾個小工具可以讓需求代碼變得更加簡潔。
準(zhǔn)備:
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、添加依賴
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.12</version>
</dependency>
</dependencies>
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、添加依賴
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
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}}文章來源:http://www.zghlxwxcb.cn/news/detail-505746.html
3.使用nio讀取json文件
3.1、添加依賴
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.12</version>
</dependency>
</dependencies>
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}文章來源地址http://www.zghlxwxcb.cn/news/detail-505746.html
到了這里,關(guān)于java— 讀取JSON文件的多種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!