Json字符串和对象,对象和json字符串,List和json,jSon和list相互转换

1.list转jsonString
List<DataModel> dataList = new ArrayList();
String respstr= JSON.toJSONString(dataList, SerializerFeature.DisableCircularReferenceDetect);
2.jsonString转list

  1. jsonStrList 为 [{
    “ms”:“xxxx”,
    “indi”:“xxxx”,
    “date”:“2018‐11‐21 12:00:00”,
    “value”:1232.45
    },{
    “ms”:“xxxx”,
    “indi”:“xxxx”,
    “date”:“2018‐11‐21 11:00:00”,
    “value”:2334.56
    }]
    List<DataModel> dataModelList = JSONObject.parseArray(jsonStrList, DataModel.class);

  2. jsonStrList 为 {
    “errorCode” : 0,
    “errorMessage” : null,
    “data” : {
    “total” : 2,
    “pageNo” : 1,
    “pageSize” : 10,
    “list” : [
    {
    “userUuid” : “xxxxx1”,
    “loginAccount” : “test1”,
    “userName” : “测试”
    },
    {
    “userUuid” : “xxxxx2”,
    “loginAccount” : “test2”,
    “userName” : “测试”
    }
    ]
    }
    }
    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    JSONObject jan = (JSONObject) jsonObject.get(“data”);
    JSONArray jsonDataList = jan.getJSONArray(“list”);
    List<DoorDevice> doorDeviceList = JSONObject.parseArray(jsonDataList.toJSONString(), DoorDevice.class);

  3. jsonStrList 为{
    “errorCode” : 0,
    “errorMessage” : null,
    “data” : “f0b93a745cc911e8b2bdd771b6792897”
    }
    JSONObject jsonObject = JSONObject.parseObject(jsonStrList);
    String DefaultUserUUID = jsonObject.getString(“data”);

  4. jsonStrList 为{
    “errorCode” : 0,
    “errorMessage” : null,
    “data” : {
    “ms”:“xxxx”,
    “indi”:“xxxx”,
    “date”:“2018‐11‐21 11:00:00”,
    “value”:12342.45
    }
    }

JSONObject jsonObject = JSONObject.parseObject(jsonStr);
JSONObject jan = (JSONObject) jsonObject.get(“data”);
EBikeStatus eBikeStatus = JSONObject.parseObject(jan.toJSONString(), EBikeStatus.class);

  1. jsonStrList 为{
    “errorCode” : 0,
    “errorMessage” : null,
    “data” : [{
    “ms”:“xxxx”,
    “indi”:“xxxx”,
    “date”:“2018‐11‐21 12:00:00”,
    “value”:12342.45
    },{
    “ms”:“xxxx”,
    “indi”:“xxxx”,
    “date”:“2018‐11‐21 11:00:00”,
    “value”:23344.56
    }]
    }
    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    JSONArray jsonDataList = jsonObject .getJSONArray(“data”);
    List<DoorDevice> doorDeviceList = JSONObject.parseArray(jsonDataList.toJSONString(), DoorDevice.class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科学的N次方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值