fastjson用法1
2015-07-23 13:00
633 查看
整理了一下fastjson的一些用法,这里选用了1.2.4版本
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
}
}
//学生类 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
}
}
相关文章推荐
- fastjson处理接口时的一个问题
- XML 与 JSON 优劣对比
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧
- .Net中的json操作类用法分析
- JavaScript中字符串(string)转json的2种方法
- js Object2String方便查看js对象内容
- C#中的DataSet、string、DataTable、对象转换成Json的实现代码
- Json实现异步请求提交评论无需跳转其他页面
- javascript中JSON对象与JSON字符串相互转换实例
- javascript格式化json显示实例分析
- jquery处理json数据实例分析