fastjson用法1

整理了一下fastjson的一些用法,这里选用了1.2.4版本

//学生类
public class Student {
	private Integer studentId;
	private String name;
	private int age;
	private Date birth;
}
 
//课程类
public class Course {
private Integer courseId;
private String courseName;
}

 
//分数类
public class Score {
private Student student;
private Course course;
private double score;
}


 
 
省略了get和set方法,无参、有参构造方法,以及toString()方法


public class JSONDemo01 {

	public static void main(String[] args) {

		Student student = new Student(1, "张三", 15, new Date());
		Course course = new Course(1, "语文");
		Score score = new Score(student, course, 90.50);

		// 把自定义的JAVA对象转化为JSON字符串
		String jsonString = JSON.toJSONString(score);
		System.out.println(jsonString);
		// 默认输出格式:对象属性按照字母排序,时间转换为毫秒数,字符串用双引号包含
		// {"course":{"courseId":1,"courseName":"语文"},"score":90.5,"student":{"age":15,"birth":1437628296881,"name":"张三","studentId":1}}

		// 把JSON字符串转化为自定义的JAVA对象
		Score score2 = JSON.parseObject(jsonString, Score.class);
		System.out.println(score2 + "\n" + score2.equals(score));
		// 自定义类型的toString()方法输出结果为Score [student=Student [studentId=1, name=张三, age=15, birth=Thu Jul 23 15:20:52 CST 2015], course=Course
		// [courseId=1, courseName=语文], score=90.0]
		// Object类的equals方法输出结果为false
	}
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值