package com.example.demo.jsonParse;
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.charset.StandardCharsets;
public class JsonReadTest {
@Test
public void parse() throws IOException {
ClassPathResource resource = new ClassPathResource("data.json");
byte[] bdata = FileCopyUtils.copyToByteArray(resource.getInputStream());
String data = new String(bdata, StandardCharsets.UTF_8);
System.out.println(data);
TestClass test = JSON.parseObject(data,TestClass.class);
System.out.println(JSON.toJSONString(test));
}
}
JAVA 中 JSON文件的读取
最新推荐文章于 2024-07-31 10:20:03 发布
该篇博客主要展示了如何在Java中使用阿里巴巴的FastJSON库来读取并解析JSON文件。在测试方法`parse()`中,从`data.json`文件中读取内容,转换为字符串,并进一步将内容反序列化为`TestClass`对象。博客内容详细阐述了这个过程,对于理解和应用FastJSON进行JSON操作具有指导意义。
摘要由CSDN通过智能技术生成