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

2 篇文章 0 订阅

依赖包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;
	}

}

Schoolmaster类

package org.ywzn;

public class Schoolmaster {

	private String name;

	public String getName() {
		return name;
	}

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

School类

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
校长甲
学生甲
学生乙

转自:http://blog.csdn.net/sjiang2142/article/details/6709908#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值