json怎么解析混合数组对象到实体类的list集合里去

一、前言

这次项目中遇到了一个这样的需求,把下面数据解析到我下面的对象里去。

json 文件

[
    {
        "categories": [
            "美食,小吃快餐"
        ],
        "coordinate": {
            "address": "王府井大街138号新东安广场3层328",
            "area": "东安市场",
            "area_id": "jk238eewf0k",
            "city": "北京市",
            "city_code": "110100",
            "district": "东城区",
            "district_code": "110101",
            "floor": "3",
            "latitude": "39.91456",
            "longitude": "116.41155",
            "province": "北京市",
            "province_code": "110000"
        },
        "name": "探鱼",
        "phone": "010-65280328",
        "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<String> 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<LocatorData> 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"}]

我们用工具转换一下看的更清晰一点:

[
    {
        "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"
    }
]
发布了332 篇原创文章 · 获赞 198 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/riemann_/article/details/103867082