项目场景:
String 转List<Entity>:
问题描述:
记录一个最近项目上踩的坑,获取第三方接口返回的数据:
将String转化成List<Entity>
数据样式为:
value = “[{“indicator”: “1324”, “lastGrade”: 1, “lastValue”: 12.21, “gradeChange”: 0, “currentGrade”: 1, “currentValue”: 12.05, “indicatorView”: “45656456”}]”
我的解决方式1如下:
List<Entity> arrayLists = JSON.parseArray(JSON.parse(value.toString()).toString(),Entity.class);
我的解决方式2如下:
先将数据转成JSONArray ,再将数据转成List<Entity>
JSONArray jsonArray = JSONArray.parseArray(value);
List<Entity> arrayLists = JSON.parseArray(jsonArray.toString(),Entity.class);