使用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

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

上一篇:Strategy Analytics:有效的学习能力对人工智能的完美体验至关重要


下一篇:Kubernetes 安装Rook 1.3.11 ceph 14.2.10