fastjson对json数组转java实体类list
实在不行就老老实实先解析出JSONArray,再for循环一个一个转成实体类
json先转字符串再转list
要转的json对象,里面有两个要转的实体类,将数组转为list
{
"oldGoodsStorageModes": [{
"ratio": 1,
"unitId": "b7f2be7ed7d84727b44ff79382985bac",
"name": "盒",
"sort": 1
}, {
"ratio": "2",
"unitId": "e7e8d204a17a4c709c4149e4d41b3538",
"sort": 1,
"name": "批"
}]
}
JSONObject jsonObject = JSONObject.parseObject(requestBody);//从请求体里获得jsonObject
String oldGoodsStorageModes = jsonObject.getString("oldGoodsStorageModes");//解析成字符串
//字符串转list
List<GoodsStoragemode> oldGoodsStoragemodes = JSONObject.parseArray(oldGoodsStorageModes,GoodsStoragemode.class);
注意:对象可能需要实现序列化
属性、构造方法省略
public class GoodsStoragemode implements Serializable{
private static final long serialVersionUID = 1L;
}
我这个自定义类一开始就序列化了,没有测试不加序列化会不会报错,看别的博客是有提到要序列化