JSON数组转成实体类集合

1、首先得到JSON数组(一般调用别人的接口,返回的数据是JSON数组)

Object data = result.getData();
System.out.println(data);

[WaterMeter(id=2061, waterMeter=字符串999, waterMeterNo=1000, acquisitionTime=Fri Mar 10 09:34:10 CST 2023, data=1000.200, currentReading=1000.300, consumption=1000.400, createTime=Fri Mar 10 00:00:00 CST 2023, updateTime=Fri Mar 10 00:00:00 CST 2023, gatherDay=null, delFlag=0)]

2、将其先转换成字符串

String s = JSON.toJSONString(data);
System.out.println(s);
                    
[{"acquisitionTime":1678412050000,"consumption":1000.400,"createTime":1678377600000,"currentReading":1000.300,"data":1000.200,"delFlag":0,"id":2061,"updateTime":1678377600000,"waterMeter":"字符串999","waterMeterNo":1000}]

3、再转换成实体类集合

List<WaterMeter> waterMeters = JSON.parseArray(s, WaterMeter.class);
System.out.println(waterMeters);

[WaterMeter(id=2061, waterMeter=字符串999, waterMeterNo=1000, acquisitionTime=Fri Mar 10 09:34:10 CST 2023, data=1000.200, currentReading=1000.300, consumption=1000.400, createTime=Fri Mar 10 00:00:00 CST 2023, updateTime=Fri Mar 10 00:00:00 CST 2023, gatherDay=null, delFlag=0)]

4、调用的接口 返回数据格式

{
    "code": "SUCCESS",
    "message": "",
    "data": [
        {
            "id": 2061,
            "waterMeter": "字符串999",
            "waterMeterNo": 1000,
            "acquisitionTime": "2023-03-10 09:34:10",
            "data": 1000.200,
            "currentReading": 1000.300,
            "consumption": 1000.400,
            "createTime": "2023-03-10 00:00:00",
            "updateTime": "2023-03-10 00:00:00",
            "gatherDay": null,
            "delFlag": 0
        }
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值