“poi_id”: “d7s8f6s6212”
},
{
“categories”: [
“美食,面包甜点”
],
“coordinate”: {
“address”: “丰台北路18号院恒泰广场F6楼”,
“area”: “恒泰广场”,
“area_id”: “ji8sgg3b32o”,
“city”: “北京市”,
“city_code”: “110100”,
“district”: “丰台区”,
“district_code”: “110106”,
“floor”: “6”,
“latitude”: “39.86608”,
“longitude”: “116.30516”,
“province”: “北京市”,
“province_code”: “110000”
},
“name”: “好客来”,
“phone”: “010-65280221”,
“poi_id”: “3g97sg8sd67”
}
]
实体对象:
public class LocatorData {
// 分类
private String categories;
// 商圈
private String area;
// 商圈id
private String areaId;
// 省份
private String province;
// 省份code
private String provinceCode;
// 城市
private String city;
// 城市code
private String cityCode;
// 区县
private String district;
// 区县code
private String districtCode;
// 楼层
private String floor;
// 经度
private Double latitude;
// 维度
private Double longitude;
// 店铺名称
private String name;
// 店铺地址
private String address;
// 店铺电话
private String phone;
// 店铺id
private String poiId;
}
不知道小伙伴有没有发现,我这里只是一个对象,没有一对多的关系哈,要的就是这样的效果。
但 json 文件里有个 coordinate 对象,而我实体对象不需要这个字段。那也没关系,一步步解析就完事了。
@RestController
public class ParseController {
private static final Logger LOGGER = LoggerFactory.getLogger(EasyExcelController.class);
@PostMapping(value = “/arrayObjectNestingParse”,produces = “application/json;charset=UTF-8”)
public ReturnT arrayObjectNestingParse(@RequestParam(value = “jsonFile”) MultipartFile jsonFile) {
if (jsonFile == null) {
return new ReturnT<>(ReturnT.BAD_REQUEST, “Params can not be null”);
}
InputStream is = null;
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
String str = null;
try {
is = jsonFile.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
while ((str = br.readLine()) != null) {
sb.append(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
List locatorDataList = new ArrayList<>();
JSONArray jsonArray = JSONArray.parseArray(sb.toString());
for (int i = 0; i < jsonArray.size(); i++) {
LocatorData locatorData = new LocatorData();
JSONObject jsonObject = jsonArray.getJSONObject(i);
String categories = jsonObject.getString(“categories”);
String coordinate = jsonObject.getString(“coordinate”);
JSONObject coordinateObj = JSONObject.parseObject(coordinate);
String address = coordinateObj.getString(“address”);
String area = coordinateObj.getString(“area”);
String areaId = coordinateObj.getString(“area_id”);
String city = coordinateObj.getString(“city”);
String cityCode = coordinateObj.getString(“city_code”);
String district = coordinateObj.getString(“district”);
String districtCode = coordinateObj.getString(“district_code”);
String floor = coordinateObj.getString(“floor”);
String latitude = coordinateObj.getString(“latitude”);
String longitude = coordinateObj.getString(“longitude”);
String province = coordinateObj.getString(“province”);
String provinceCode = coordinateObj.getString(“province_code”);
String name = jsonObject.getString(“name”);
String phone = jsonObject.getString(“phone”);
String poiId = jsonObject.getString(“poi_id”);
locatorData.setCategories(categories);
locatorData.setAddress(address);
locatorData.setArea(area);
locatorData.setAreaId(areaId);
locatorData.setProvince(province);
locatorData.setProvinceCode(provinceCode);
locatorData.setCity(city);
locatorData.setCityCode(cityCode);
locatorData.setDistrict(district);
locatorData.setDistrictCode(districtCode);
locatorData.setFloor(floor);
locatorData.setLatitude(Double.parseDouble(latitude));
locatorData.setLongitude(Double.parseDouble(longitude));
locatorData.setName(name);
locatorData.setPhone(phone);
locatorData.setPoiId(poiId);
locatorDataList.add(locatorData);
}
LOGGER.info("locatorDataList: " + JSON.toJSONString(locatorDataList));
return new ReturnT<>(ReturnT.SUCCESS, JSON.toJSONString(locatorDataList));
}
}
日志打印:
2020-01-07 00:39:30.284 [http-nio-8080-exec-3] INFO c.r.springbootdemo.controller.EasyExcelController-locatorDataList: [{“address”:“王府井大街138号新东安广场3层328”,“area”:“东安市场”,“areaId”:“jk238eewf0k”,“categories”:“[“美食,小吃快餐”]”,“city”:“北京市”,“cityCode”:“110100”,“district”:“东城区”,“districtCode”:“110101”,“floor”:“3”,“latitude”:39.91456,“longitude”:116.41155,“name”:“探鱼”,“phone”:“010-65280328”,“poiId”:“d7s8f6s6212”,“province”:“北京市”,“provinceCode”:“110000”},{“address”:“丰台北路18号院恒泰广场F6楼”,“area”:“恒泰广场”,“areaId”:“ji8sgg3b32o”,“categories”:“[“美食,面包甜点”]”,“city”:“北京市”,“cityCode”:“110100”,“district”:“丰台区”,“districtCode”:“110106”,“floor”:“6”,“latitude”:39.86608,“longitude”:116.30516,“name”:“好客来”,“phone”:“010-65280221”,“poiId”:“3g97sg8sd67”,“province”:“北京市”,“provinceCode”:“110000”}]
我们用工具转换一下看的更清晰一点:
最后
给大家送一个小福利
附高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、准备面试跳槽、自身职业规划迷茫的朋友们。
608,“longitude”:116.30516,“name”:“好客来”,“phone”:“010-65280221”,“poiId”:“3g97sg8sd67”,“province”:“北京市”,“provinceCode”:“110000”}]
我们用工具转换一下看的更清晰一点:
最后
给大家送一个小福利
[外链图片转存中…(img-6R8T6k6x-1721144703371)]
附高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、准备面试跳槽、自身职业规划迷茫的朋友们。
[外链图片转存中…(img-ZLJATsra-1721144703372)]