使用fastJSON
来源:互联网 发布:使命召唤9剧情知乎 编辑:程序博客网 时间:2024/05/19 16:48
package com.ylchou.testfastjson;public class Person { private Student student; private Teacher teacher; public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } }
package com.ylchou.testfastjson;public class Student {private int id;private String name;private int age;private Dog dog;public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
package com.ylchou.testfastjson;public class Teacher {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
package com.ylchou.testfastjson;public class Dog { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
package com.ylchou.testfastjson;public class Dog {private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
package com.ylchou.testfastjson;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;public class TestFastJSON { public static void p(String str){ System.out.println(str); } public static void main(String[] args) { Long time1 = System.currentTimeMillis(); Student student = new Student(); Teacher teacher = new Teacher(); Person person = new Person(); Dog dog = new Dog(); student.setAge(12); student.setId(001); student.setName("ylchou"); student.setDog(dog); teacher.setAge(25); teacher.setId(007); teacher.setName("lili"); person.setStudent(student); person.setTeacher(teacher); dog.setAge(2); dog.setId(999); dog.setName("小黄"); String studentJson = JSON.toJSONString(student, true); String studentJson2 = JSON.toJSONString(student, false); String studentJson3 = JSON.toJSONString(student);//默认为false p("studentJson---------------------------------------"); p(studentJson);p(""); p("studentJson2---------------------------------------"); p(studentJson2);p(""); p("studentJson3---------------------------------------"); p(studentJson3);p(""); String teacherJson = JSON.toJSONString(teacher, true); p("teacherJson---------------------------------------"); p(teacherJson);p(""); String personJson = JSON.toJSONString(person, true); p("personJson---------------------------------------"); p(personJson);p(""); String dogJson = JSON.toJSONString(dog, true); p("dogJson---------------------------------------"); p(dogJson);p(""); List<Object> list = new ArrayList<Object>(); list.add("ylchou"); list.add("lili"); list.add("小黄"); list.add(dog); list.add(teacher); list.add(student);//有问题 list.add(person);//有问题 String listJson = JSON.toJSONString(list, true); p("listJson---------------------------------------"); p(listJson);p(""); Map<String,Object> map = new HashMap<String, Object>(); map.put("ylchou", "湖北"); map.put("小黄", dog); String mapJson = JSON.toJSONString(map, true); p("mapJson----------------------------------------"); p(mapJson);p(""); Set<Object> set = new HashSet<Object>(); set.add("ylcohu"); set.add(student); set.add(person); String setJson = JSON.toJSONString(set,true); p("setJson----------------------------------------"); p(setJson);p(""); String mapToArray = JSONArray.toJSONString(map, true); p("mapToArray----------------------------------------"); p(mapToArray);p(""); Long time2 = System.currentTimeMillis(); System.out.println("所用时间:"+(time2-time1)+"毫秒"); }}
控制台打印结果:
studentJson---------------------------------------
{
"age":12,
"dog":{
"age":2,
"id":999,
"name":"小黄"
},
"id":1,
"name":"ylchou"
}
studentJson2---------------------------------------
{"age":12,"dog":{"age":2,"id":999,"name":"小黄"},"id":1,"name":"ylchou"}
studentJson3---------------------------------------
{"age":12,"dog":{"age":2,"id":999,"name":"小黄"},"id":1,"name":"ylchou"}
teacherJson---------------------------------------
{
"age":25,
"id":7,
"name":"lili"
}
personJson---------------------------------------
{
"student":{
"age":12,
"dog":{
"age":2,
"id":999,
"name":"小黄"
},
"id":1,
"name":"ylchou"
},
"teacher":{
"age":25,
"id":7,
"name":"lili"
}
}
dogJson---------------------------------------
{
"age":2,
"id":999,
"name":"小黄"
}
listJson---------------------------------------
[
"ylchou",
"lili",
"小黄",
{
"age":2,
"id":999,
"name":"小黄"
},
{
"age":25,
"id":7,
"name":"lili"
},
{
"age":12,
"dog":{"$ref":"$[3]"},
"id":1,
"name":"ylchou"
},
{
"student":{"$ref":"$[5]"},
"teacher":{"$ref":"$[4]"}
}
]
mapJson----------------------------------------
{"ylchou":"湖北","小黄":{
"age":2,
"id":999,
"name":"小黄"
}}
setJson----------------------------------------
[{
"student":{
"age":12,
"dog":{
"age":2,
"id":999,
"name":"小黄"
},
"id":1,
"name":"ylchou"
},
"teacher":{
"age":25,
"id":7,
"name":"lili"
}
},{"$ref":"$[0].student"},"ylcohu"]
mapToArray----------------------------------------
{"ylchou":"湖北","小黄":{
"age":2,
"id":999,
"name":"小黄"
}}
所用时间:93毫秒
- 使用fastJSON
- FastJson使用
- FastJSON使用
- fastJson使用
- fastjson 使用
- fastjson 使用
- FastJson使用
- fastjson使用
- fastJSON使用
- FastJson 使用
- FastJson使用
- FastJson使用
- fastjson使用
- FastJson使用
- Fastjson使用
- FastJson 使用
- fastjson使用
- fastjson使用
- 嵌入式Web服务器
- vsftpd 入门
- Turbo C简单使用教程
- Systemverilog枚举类型 ---转自ic designer
- java内存分配机制
- 使用fastJSON
- iPhone CFSocketCreate的各字段含义
- jbpm几张表简单介绍
- 看门狗
- jbpm的简单例子
- 如何重启或关闭远程服务器
- 二句话就可治疗颈椎病
- jbpm总结1
- jbpm总结1