首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >JSON字符串转换List对象列表 JSONArray toJavaList

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

作者头像
oktokeep
发布2025-09-17 08:22:27
发布2025-09-17 08:22:27
19500
代码可运行
举报
文章被收录于专栏:第三方工具第三方工具
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
复制
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;
        }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-09-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档