wordpress怎样做单页网站,中信建设有限责任公司招标,个人简历网免费模板,wordpress弹出框插件将json数据转为自己的实体model 要转化#xff0c;首先要明白自己拿到的json是什么格式#xff0c;想要转换为什么格式 json中 map#xff08;以{}包着#xff09;就是一个对象#xff0c;list#xff08;以[]包着#xff09;就是一个数组 看清楚自己的json数据结构是否…将json数据转为自己的实体model 要转化首先要明白自己拿到的json是什么格式想要转换为什么格式 json中 map以{}包着就是一个对象list以[]包着就是一个数组 看清楚自己的json数据结构是否和如下的一致一致方法才能用
第一种json格式json数据是一个Map将Map中某个值中的数据转化为model
// json map格式 把testStuData1转化为ListStudent格式
{testStuData1: [{name:张三,age:22,score: [{math: 77,english: 67}]}, {name:李四,age:21,score: [{math: 57,english: 97}]}]
}
//Student实体类 必须和json中要转化 字段名一一对应类型也必须对应
public class Student{private String name;private String age;private ListString score;
}
//转化代码 stuList就会拿取到跟json中对应的数据
try{ObjectMapper objectMapper new ObjectMapper();ListStudent stuList JSONArray.parseArray(JSONArray.toJSONString(map.get(testStuData1)), Student.class);
}catch (Exception e) {}
第二种json格式json数据是一个 List 整个List转化为model
// json list格式 把整个json数据转化为ListStudent格式[{name:张三,age:22,score: [{math: 77,english: 67}]}, {name:李四,age:21,score: [{math: 57,english: 97}]}
]
//Student实体类 必须和json中要转化 字段名一一对应类型也必须对应
public class Student{private String name;private String age;private ListString score;
}
//转化代码 stuList就会拿取到跟json中对应的数据
try{ObjectMapper objectMapper new ObjectMapper();List list objectMapper.readValue(dataJson, List.class);ListStudent stuListJSONObject.parseArray(JSONArray.toJSONString(list), Student.class);
}catch (Exception e) {}