使用Gson解析含有集合的json数据

json数据

{
    "messageCode": "00000",
    "message": "统计停车场枪口空闲及总数量成功!",
    "data": [
        {
            "fastChargeLeisure": 0,
            "tatolFastCharge": 15,
            "slowChargeLeisure": 2,
            "tatolSlowCharge": 2,
            "carparkId": "1424"
        },
       {
            "fastChargeLeisure": 5,
            "tatolFastCharge": 17,
            "slowChargeLeisure": 4,
            "tatolSlowCharge": 3,
            "carparkId": "1425"
        }
    ]
}

Map map = gson.fromJson(json, Map.class);
List<Map> data = (List<Map>)map.get("data");
for(Map d:data) {
double fastChargeLeisure = (double) d.get("fastChargeLeisure");
double tatolFastCharge = (double) d.get("tatolFastCharge");
double slowChargeLeisure = (double) d.get("slowChargeLeisure");
double tatolSlowCharge = (double) d.get("tatolSlowCharge");
long carparkId = Long.parseLong((String) d.get("carparkId"));
carParkResults_List.forEach((carParkMobileResponseDTO) -> {
      if (carParkMobileResponseDTO.getCarParkId().equals(carparkId)){
            carParkMobileResponseDTO.setEvFreeLots((int)(fastChargeLeisure + slowChargeLeisure));
            carParkMobileResponseDTO.setEvTotalLots((int)(tatolFastCharge + tatolSlowCharge));
            }
      });
}

注意:使用gson.fromJson解析成map对象,其中Integer类型会默认转换为Double

posted @ 2021-01-06 09:29  小老弟1999  阅读(407)  评论(0编辑  收藏  举报