zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    python基本数据类型之字符串(二)
    python基本数据类型之字符串(一)
    Java基础之Java简介
    1024lab-How to run project using .ipynb
    pytorch调试工具
    关于深度学习选择和使用GPU
    中文文本预处理
    GCN相关
    Bert project Debug记录
    图卷积相关的参考
  • 原文地址:https://www.cnblogs.com/xiaolaodi1999/p/14239218.html
Copyright © 2011-2022 走看看