fastJson
1.将对象(包含JSONObject对象)转换成JSONObject对象:
JSON.toJSON(Object obj);
2.将对象(包含JSONObject对象)转换成JSON字符串:
JSON.toJSONString(Object obj);
3.将对象(包含JSONObject对象)转换成标准格式的json字符串:
JSON.toJSONString(Object obj,true);
4.序列化部分字段:
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(实体类.class, "字段1","字段2")
String json =JSON.toJSONString(Object obj,filter);
5.将json字符串转化成class类
JSON.parseObject(String text, Class<T> clazz)
6.将json字符串转化成JSONObject对象
JSON.parseObject(String text)
package com.sxlg.fastJson.model; /** * 严格的一个JavaBean组件 */ public class Student { public Student(int id,String name,int age) { this.id = id; this.name = name; this.age = age; } 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; } @Override public String toString() { return "Student=[id="+this.id+",age="+this.age+",name="+this.name+"]"; } }
package com.sxlg.fastJson.model; import java.util.List; /** * 严格的一个JavaBean组件 */ public class Teacher { public Teacher() { } public Teacher(int id,String name,List<Student> students) { this.id = id; this.name = name; this.students = students; } private int id; private String name; private List<Student> students; 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 List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
package com.sxlg.fastJson; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; import com.sxlg.fastJson.model.Student; import com.sxlg.fastJson.model.Teacher; import java.util.ArrayList; import java.util.List; /** * JOSNObject 对象 * JSONArray 对象 * JSON 提供json对象和字符串转换的常用方法 */ public class Test { public static void main(String[] args) { Student student1 = new Student(1,"GalHa",25); Student student2 = new Student(2,"GalHa",25); Student student3 = new Student(3,"GalHa",25); List<Student> list = new ArrayList<Student>(); list.add(student1); list.add(student2); list.add(student3); Teacher teacher = new Teacher(1,"sadw",list); // 将对象转换成JSONObject对象 JSONObject json = (JSONObject) JSON.toJSON(student1); // 将对象转换成json字符串 String js = JSON.toJSONString(student1); // 将对象转换成JSONObject对象 JSONObject json1 = (JSONObject) JSON.toJSON(teacher); // 将对象转换成标准格式的json字符串 String js1 = JSON.toJSONString(teacher,true); // 序列化部分字段 SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","age"); String jsonStu =JSON.toJSONString(student1,filter); //System.out.println(json); //System.out.println(js); //System.out.println(json1); //System.out.println(js1); //System.out.println("将json字符串转换成实体类:"+JSON.parseObject(js1,Student.class)); System.out.println("将json字符串转换成JSONObject对象:"+JSON.parseObject(js1)); } }