使用fastJson将json转换成javaBean集合(list<T>)

废话不说,上json串

jsonStr = [{"MATERIAL_ID_":"10000021020033","MATERIAL_NO_":"051001175-3213-00002","MATERIAL_CLASSIFY_":"钻头","DESCRIPTION_":"【长度:15】","UNIT_":"付","PLAN_TO_USE_":20},{"MATERIAL_ID_":"10000021020038","MATERIAL_NO_":"051001150-213132-00003","MATERIAL_CLASSIFY_":"石头","DESCRIPTION_":"【长度:213】","UNIT_":"吨","PLAN_TO_USE_":200},{"MATERIAL_ID_":"10000030931347","MATERIAL_NO_":"050502001-SDDSA-00006","MATERIAL_CLASSIFY_":"木头","DESCRIPTION_":"【长度:213】","UNIT_":"根","PLAN_TO_USE_":150},{"PLAN_TO_USE_":"","DESCRIPTION_":"【长度:123】","UNIT_":"公斤","MATERIAL_NO_":"051001175-333-00001","MATERIAL_CLASSIFY_":"钻头","MATERIAL_ID_":"10000021020030"}]

这个json里有三条数据,往往需要将其转换成list集合,这里使用fastJson,只需要两行代码

JSONArray array = JSONArray.parseArray(jsonStr);
List<MsProposedVo> mpvList = JSONObject.parseArray(array.toJSONString(), MsProposedVo.class);

MsProposedVo 实体类中的属性命名(特殊情况这么写了,正常情况下建议使用小写、驼峰命名法)

着重注意,json串里的字段要和list的泛型的对象的字段一致,否则调试时,上述写法只会输出对应个数的集合,但是集合里的字段值都为null

(0)

相关推荐