一.利用ObjectMapper將json字符串轉爲Listjava
Student.javanode
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.javajson
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.javaapp
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字符串轉Listthis
Test.javaspa
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()); } }