
正文
ObjectMapper将josn字符串转化为List
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示

一.利用ObjectMapper将json字符串转为List
Student.java
package objectmapper;import java.io.Serializable;public class Student{
private Integer id;
private String sName;
private String sAddress;
private Double sal; public Student() {
}
public Student(Integer id, String sName, String sAddress, Double sal) {
super();
this.id = id;
this.sName = sName;
this.sAddress = sAddress;
this.sal = sal;
}
//get/set}
ZcjUser.java
package objectmapper;public class ZcjUser implements Serializable { private static final long serialVersionUID = 1L;
private int id;
private String message;
private Date sendTime; // 这里手写字母大写,只是为了测试使用,是不符合java规范的
private String NodeName;
private List<Integer> intList;
private List<Student> studentList;
public ZcjUser(int id, String message, Date sendTime) {
super();
this.id = id;
this.message = message;
this.sendTime = sendTime;
}
//get/set
}
Test.java
package objectmapper;public class Test {
public static ObjectMapper mapper = new ObjectMapper(); static {
// 转换为格式化的json
mapper.enable(SerializationFeature.INDENT_OUTPUT); // 如果json中有新增的字段并且是实体类类中不存在的,不报错
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
} public static void main(String[] args) throws Exception {
testObj();
}
public static void testObj() throws JsonGenerationException, JsonMappingException, IOException {
ZcjUser user = new ZcjUser(1, "Hello World", new Date()); List<Integer> intList = new ArrayList<>();
intList.add(11);
intList.add(22);
intList.add(33);
user.setIntList(intList); List<Student> sList = new ArrayList<>();
Student s1 = new Student(1, "zs", "四川", 111.1);
Student s2 = new Student(2, "ls", "成都", 222.2);
Student s3 = new Student(3, "ww", "兴隆湖", 333.3);
sList.add(s1);
sList.add(s2);
sList.add(s3);
user.setStudentList(sList); List<ZcjUser> userList = new ArrayList<>();
userList.add(user); mapper.writeValue(new File("D:/test3.txt"), userList); // 写到文件中 List<ZcjUser> lendReco = mapper.readValue(new File("D:/test3.txt"),new TypeReference<List<ZcjUser>>() { });
System.out.println(lendReco.get(0).getStudentList().get(0).getsName()); }}
test3.txt
[ {
"id" : 1,
"message" : "Hello World",
"sendTime" : 1557823326329,
"intList" : [ 11, 22, 33 ],
"studentList" : [ {
"id" : 1,
"sName" : "zs",
"sAddress" : "四川",
"sal" : 111.1
}, {
"id" : 2,
"sName" : "ls",
"sAddress" : "成都",
"sal" : 222.2
}, {
"id" : 3,
"sName" : "ww",
"sAddress" : "兴隆湖",
"sal" : 333.3
} ],
"nodeName" : null
} ]
二。利用JSONArray将json字符串转List
Test.java
public class Test { public static void main(String[] args) throws Exception {
testObj();
}
public static void testObj() throws JsonGenerationException, JsonMappingException, IOException {
String jsonStr = FileUtils.readFileToString(new File("D:/test3.txt"), "utf-8"); Map<String,Class<Student>> map = new HashMap<String,Class<Student>>();
// studentList 为ZcjUser的属性
map.put("studentList", Student.class);
JSONArray array = JSONArray.fromObject(jsonStr);
List<ZcjUser> list2 = (List<ZcjUser>) JSONArray.toList(array, ZcjUser.class,map); System.out.println(list2.get(0).getStudentList().get(0).getsName());
}}







