java读取json文件并解析成对象
json文件格式
[{
"angle": 1.0002098306294742,
"id": 1,
"lat": 30.479762556551492,
"lon": 114.31612000661855,
"speed": 36
}, {
"angle": 1.0002098306294742,
"id": 2,
"lat": 30.479758174655966,
"lon": 114.31611998912472,
"speed": 36
}, {
"angle": 1.0002098306296752,
"id": 3,
"lat": 30.47975379276045,
"lon": 114.3161199716309,
"speed": 36
}]
代码如下
Location location = new Location();
File file =new File("C:\\Users\\YJKJ\\Desktop\\result1.json");
String json = FileUtils.readFileToString(file, "UTF-8");
List<Location> list = JSON.parseObject(json, new TypeReference<List<Location>>() {});
for (int i = 0; i < list.size(); i++) {
BeanUtil.copyProperties(list.get(i),location);
}