依赖包jsontools-core-1.7.jar,antlrworks-2.7.7.jar
两个jar打包下载地址:http://download.csdn.net/detail/u014175572/8796675
如果使用的是Maven:
<dependency> <groupId>com.sdicons.jsontools</groupId> <artifactId>jsontools-core</artifactId> <version>1.7</version> </dependency>
实现代码:
Student类
package org.ywzn; public class Student { private Integer id; private String name; private String sex; private Integer age; private String[] hobby; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String[] getHobby() { return hobby; } public void setHobby(String[] hobby) { this.hobby = hobby; } }
package org.ywzn; public class Schoolmaster { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package org.ywzn; import java.util.Date; import java.util.List; public class School { private String name; private List<Student> list; private Date createDate; private Schoolmaster schoolmaster; public List<Student> getList() { return list; } public void setList(List<Student> list) { this.list = list; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Schoolmaster getSchoolmaster() { return schoolmaster; } public void setSchoolmaster(Schoolmaster schoolmaster) { this.schoolmaster = schoolmaster; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package org.ywzn; import java.io.StringReader; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import antlr.RecognitionException; import antlr.TokenStreamException; import com.sdicons.json.mapper.JSONMapper; import com.sdicons.json.mapper.MapperException; import com.sdicons.json.model.JSONValue; import com.sdicons.json.parser.JSONParser; public class Main { @SuppressWarnings("rawtypes") public static void main(String[] args) throws TokenStreamException, RecognitionException { List<Student> list = new ArrayList<Student>(); Student stu1 = new Student(); stu1.setName("学生甲"); Student stu2 = new Student(); stu2.setName("学生乙"); list.add(stu1); list.add(stu2); Schoolmaster schoolmaster = new Schoolmaster(); schoolmaster.setName("校长甲"); School school = new School(); school.setCreateDate(new Date()); school.setList(list); school.setName("清华大学"); school.setSchoolmaster(schoolmaster); try { // JAVA对象转换成JSON值 JSONValue jsonValue = JSONMapper.toJSON(school); String jsonStr = jsonValue.render(true); // 是否格式化 System.out.println(jsonStr); String str = "{\"createDate\":\"2015-06-05T18:43:07.060\",\"list\":[{\"age\":null,\"hobby\":null,\"id\":null,\"name\":\"学生甲\",\"sex\":null},{\"age\":null,\"hobby\":null,\"id\":null,\"name\":\"学生乙\",\"sex\":null}],\"name\":\"清华大学\",\"schoolmaster\":{\"name\":\"校长甲\"}}"; // JSON值转换成JAVA对象 JSONParser parser1 = new JSONParser(new StringReader(str)); JSONValue jsonValue1 = parser1.nextValue(); School sch = (School) JSONMapper.toJava(jsonValue1, School.class); System.out.println(sch.getName()); System.out.println(sch.getCreateDate()); System.out.println(sch.getSchoolmaster().getName()); List<Student> stuList = sch.getList(); Iterator iter = stuList.iterator(); while (iter.hasNext()) { Student stu = (Student) iter.next(); System.out.println(stu.getName()); } } catch (MapperException e) { e.printStackTrace(); } } }
{ "createDate" : "2015-06-11T16:53:43.031", "list" : [ { "age" : null, "hobby" : null, "id" : null, "name" : "学生甲", "sex" : null }, { "age" : null, "hobby" : null, "id" : null, "name" : "学生乙", "sex" : null } ], "name" : "清华大学", "schoolmaster" : { "name" : "校长甲" } } 清华大学 Fri Jun 05 18:43:07 CST 2015 校长甲 学生甲 学生乙