import java.io.*;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class TestByJson {
//读取json文件
public static String readJsonFile(String fileName) {
String jsonStr = "";
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 static void main(String[] args) {
//获取json文件路径
String path =
TestByJson.class.getClassLoader().getResource("test.json").getPath();
//获取json文件数据转成Stirng型
String s = readJsonFile(path);
JSONObject jobj = JSON.parseObject(s);
List<DigiwinAimMessage> digiwinAimMessageList =
JSON.parseArray(jobj.getString("testJson"), DigiwinAimMessage.class);
System.out.println("这边写你要输出的内容");
}
}
读取本地JSON并封装到实体类中
于 2022-04-28 10:31:36 首次发布