【第17篇】通过fastjson去操作对象数据并处理json数据

package ivyy.taobao.com.entity;

import java.io.Serializable;

/**
 *@Author:liangjl
 *@Date:2014-12-19
 *@Version:1.0
 *@Description:
 */
public class Student implements Serializable{
	private Integer age;
	private String sex;
	private String userName;
	private String birthday;
	private String address;
	private String email;
	
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}

package ivyy.taobao.com.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *@Author:liangjl
 *@Date:2014-12-19
 *@Version:1.0
 *@Description:
 */
public class Classz implements Serializable{

	private List<Student> students=new ArrayList<Student>();

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}
}

package ivyy.taobao.com.domain.fastjson;

import ivyy.taobao.com.entity.Classz;
import ivyy.taobao.com.entity.Student;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


/**
 * @Author:jilongliang
 * @Date:2014-12-19
 * @Version:1.0
 * @Description:通过fastjson去操作对象数据并处理json数据
 */
public class JsonTest2 {
	public static void main(String[] args) { 
		
		//1、创建一个Student对象
		Student stu1=new Student();
		stu1.setAge(22);
		stu1.setUserName("xiaoliang");
		stu1.setSex("男");
		//2、添加对象到List<Student>泛型
		Classz claz1=new Classz();
		claz1.getStudents().add(stu1);
		//3、转成成JSON字符串
		String jsonStr=JSON.toJSONString(claz1);
		//4、创建一个JSONObject对象
		JSONObject jsonObj=new JSONObject();
		//5、转成成json对象
		Object obj=jsonObj.parse(jsonStr);
		
		System.out.println(obj);
		//6、转成成Classz对象
		Classz clz=JSON.parseObject(jsonStr, Classz.class);
		//7、拿出第0个Student对象
		Student st=clz.getStudents().get(0);
		System.out.println(st.getSex());
		
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱学习的蹭蹭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值