jsontools实现java对象与json值之间互相转换

依赖包jsontools-core-1.6.jar,antlrworks-1.4.3.jar

实现代码:

Student类

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;
	}

}

Schoolmaster类

public class Schoolmaster {

	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

School类

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;
	}
}

测试代码

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 MainClass1 {

	@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);
			//JSON值转换成JAVA对象
			JSONParser parser1 = new JSONParser(new StringReader(jsonStr));  
			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();
		}
	}
}





 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sjiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值