try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = reader.read()) != -1) {
sb.append((char) ch);
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
public class Hello {
public static void main(String[] args) throws Exception {
String path = Hello.class.getClassLoader().getResource("test.json").getPath();
String s = ReadUtils.readJsonFile(path);
JSONObject jobj = JSON.parseObject(s);
System.out.println("name" + jobj.get("name"));
JSONObject address1 = jobj.getJSONObject("address");
String street = (String) address1.get("street");
String city = (String) address1.get("city");
String country = (String) address1.get("country");
System.out.println("street :" + street);
System.out.println("city :" + city);
System.out.println("country :" + country);
JSONArray links = jobj.getJSONArray("links");
for (int i = 0 ; i < links.size(); i++) {
JSONObject key1 = (JSONObject)links.get(i);
String name = (String)key1.get("name");
String url = (String)key1.get("url");
System.out.println(name);
System.out.println(url);
pom依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version></dependency>读取JSON工具类/*** 读取json文件,返回json串* @param fileName* @return*/public ..
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
这篇文章主要讲讲 通过java去解析不同地方的json文件
通常我们需要解析本地的json文件或者服务器上的json文件。我们用来解析json格式的jar包有很多,jackson,fastjson,gson都行。但本人喜欢用fastjson。所以本篇都是以fastjson来解析json文件。
1.解析本地json文件
随便把一个json文件存储在本地的一个文件夹下,然后通过文件流将json文件内容读取出来。
然后转换成String,最后转json对象,然后再解析,获取自己想要的数据。
首先我们这个json文
在开发过程中有时会遇到需要读取本地.json文件的需求,通常会自己写Reader代码去读,但是这么做写出来的代码有些繁琐(需要关流、创建StringBuilder对象等操作)。最近发现几个小工具可以让需求代码变得更加简洁。
json文件:F:\halon.json
"ID": 10001,
"detail": "detail",
"json_format_version": 1.0,
"other_info": {
"array_one": [
[855, 410],
public class TestCode {
public static void main(String[] args) throws Exception,IOException{
// testCode();
// getFile...
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;
import java.io.IOException;
import java.nio.char
Java可以使用多种库来读取本地JSON文件,其中最常用的是Fastjson和Jackson。
Fastjson是一个由阿里巴巴开发的高性能的JSON处理器,它支持将JSON字符串转换为Java对象,以及将Java对象转换为JSON字符串。Fastjson的使用非常方便,只需要引入相关的包,并使用JSON类中的parseObject方法读取JSON文件即可。示例代码如下所示:
import com.alibaba.fastjson.JSON;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ReadJSONFile {
public static void main(String[] args) throws IOException {
String filePath = "D:/data.json";
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
String jsonString = new String(bytes, StandardCharsets.UTF_8);
JSON.parseObject(jsonString);
Jackson是一个广泛使用的JSON库,它可以实现JSON和Java对象之间的相互转换,以及JSON的读写操作。与Fastjson类似,使用Jackson读取本地JSON也很简单,只需使用ObjectMapper类实例化一个对象,并使用readValue方法读取JSON文件即可。示例代码如下所示:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ReadJSONFile {
public static void main(String[] args) throws IOException {
String filePath = "D:/data.json";
ObjectMapper mapper = new ObjectMapper();
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
mapper.readValue(bytes, Object.class);
无论是使用Fastjson还是Jackson,读取本地JSON都非常方便。读取后,可以使用Java中的其他工具对JSON进行解析、操作或者序列化。