当前位置: 首页 > news >正文

JSON字符串转换List对象列表 JSONArray toJavaList

JSON字符串转换List对象列表 JSONArray toJavaList

JSON字符串转换List对象列表 JSONArray toJavaList

package com.example.core.mydemo.java3.jsonDemo;import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.core.mydemo.json2.GsonUtils;import java.util.List;public class Testjson {public static void main(String[] args) {String resultJsonData = "{\"code\":\"200\",\"message\":\"success\",\"success\":true,\n" +"\"data\":[\n" +"{\"countryCode\":\"CN\",\"provinceId\":1,\"provinceName\":\"北京\",\"id\":1,\"code\":\"BJS\",\"name\":\"北京\",\"isHot\":false,\"firstChar\":\"B\",\"fullPinYin\":\"Beijing\",\"shortPinYin\":\"BJ\"},\n" +"{\"countryCode\":\"CN\",\"provinceId\":12,\"provinceName\":\"陕西\",\"id\":10,\"code\":\"SIA\",\"name\":\"西安\",\"isHot\":false,\"firstChar\":\"X\",\"fullPinYin\":\"Xian\",\"shortPinYin\":\"XA\"},\n" +"{\"countryCode\":\"CN\",\"provinceId\":13,\"provinceName\":\"甘肃\",\"id\":100,\"code\":\"LHW\",\"name\":\"兰州\",\"isHot\":false,\"firstChar\":\"L\",\"fullPinYin\":\"Lanzhou\",\"shortPinYin\":\"LZ\"}\n" +"]}";BaseResponse<JSONArray> response = JSONObject.parseObject(resultJsonData, BaseResponse.class);if(response != null && "200".equals(response.getCode())){JSONArray data = response.getData();
//            List<CityEntity> list = JSONObject.parseArray(data.toJSONString(), CityEntity.class);List<CityEntity> list = data.toJavaList(CityEntity.class);for (CityEntity entity:list) {System.out.println("entity=" + GsonUtils.toJson(entity));}}//以下写法错误,报错:Exception in thread "main" java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.example.core.mydemo.java3.jsonDemo.CityEntity/*BaseResponse<List<CityEntity>> response = JSONObject.parseObject(resultJsonData, BaseResponse.class);if(response != null && "200".equals(response.getCode())){List<CityEntity> list = response.getData();for (CityEntity entity:list) {System.out.println("entity=" + GsonUtils.toJson(entity));}}*/}
}package com.example.core.mydemo.java3.jsonDemo;public class BaseResponse<T> {String code;  //返回状态String  message; //错误信息boolean success; //是否成功T data;   //响应内容public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public boolean isSuccess() {return success;}public void setSuccess(boolean success) {this.success = success;}public T getData() {return data;}public void setData(T data) {this.data = data;}
}package com.example.core.mydemo.java3.jsonDemo;import java.io.Serializable;/*** 中国城市基本信息表* */
public class CityEntity implements Serializable {private static final long serialVersionUID = 1L;/*** 城市Id*///@AutoDocProperty(value="城市Id",required=true)private Integer id;/*** 省份Id*///@AutoDocProperty(value="省份Id",required=true)private Integer provinceId;/*** 省份名称*///@AutoDocProperty(value="省份名称",required=true)private String provinceName;/*** 城市编码*///@AutoDocProperty(value="城市编码",required=true)private String code;/*** 城市名称*///@AutoDocProperty(value="城市名称",required=true)private String name;/*** 是否热门城市*///@AutoDocProperty(value="是否热门城市",required=true)private Integer isHot;/*** 城市名称首字母*///@AutoDocProperty(value="城市名称首字母",required=true)private String firstChar;/*** 全拼音*///@AutoDocProperty(value="全拼音",required=true)private String fullPinYin;/*** 简拼*///@AutoDocProperty(value="简拼",required=true)private String shortPinYin;/*** 国家code*///@AutoDocProperty(value="国家code",required=true)private String countryCode;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Integer getProvinceId() {return provinceId;}public void setProvinceId(Integer provinceId) {this.provinceId = provinceId;}public String getProvinceName() {return provinceName;}public void setProvinceName(String provinceName) {this.provinceName = provinceName;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getIsHot() {return isHot;}public void setIsHot(Integer isHot) {this.isHot = isHot;}public String getFirstChar() {return firstChar;}public void setFirstChar(String firstChar) {this.firstChar = firstChar;}public String getFullPinYin() {return fullPinYin;}public void setFullPinYin(String fullPinYin) {this.fullPinYin = fullPinYin;}public String getShortPinYin() {return shortPinYin;}public void setShortPinYin(String shortPinYin) {this.shortPinYin = shortPinYin;}public String getCountryCode() {return countryCode;}public void setCountryCode(String countryCode) {this.countryCode = countryCode;}
}

 

http://www.wxhsa.cn/company.asp?id=5499

相关文章:

  • vue3 使用 docx-preview 预览 Word文档
  • 数据库原理-第三章——SQL
  • 啥是CPU
  • C# Avalonia 15- Animation- CodeAnimation
  • ubuntu 18.04安装mysql 8.0.41
  • Topaz Photo AI Pro 4.0.4 AI图片智能降噪(win版)
  • 阿里云基础设施 AI Tech Day AI 原生,智构未来——AI 原生架构与企业实践专场
  • 实用指南:LINUX910 CENTOS8 新建虚拟机;重设root密码/时间同步
  • 零基础学习PYthon记录
  • C++ std::unordered_set
  • 如何将一个项目同时提交到GitHub和Gitee(码云)上
  • 基于Matlab的LeNet-5车牌字符识别系统实现
  • MATLAB的交通标志牌识别实现
  • Python常见的数据结构和代码示例
  • Grafana 中文入门教程 | 构建你的第一个仪表盘
  • Gitee DevOps:中国开发者效率革命的数字引擎
  • Topaz Photo AI Pro 4.0.4 AI图片智能降噪
  • C++ std::map
  • 易基因:Nat Genet/IF29:董朝斌团队ChIP-seq等揭示作物株型穗型发育调控新机制 助力表观遗传育种驯化改良(顶刊佳作)
  • Edge浏览器网页长截图
  • Python TensorFlow的CNN-LSTM-GRU集成模型在边缘物联网数据IoT电动汽车充电站入侵检测应用
  • C++多线程编程—线程控制、同步与互斥详解
  • MySQL启动失败:mysqld.log Permis 报错处理.250916
  • 源码管理—密钥硬编码问题
  • 无速度传感器交流电机的扩展Luenberger观测器
  • AI Ping体验记:终于有人做大模型服务的“性能监控”了
  • 数据库原理-第二章——关系型数据库
  • mac 的任务栏 Windows-Style Taskbar For macOS
  • 快手Java一面
  • 详细介绍:Elastic APM 入门指南:快速设置应用性能监控