//第一个案例:java转换成JSON字符串
@Test
public void fromObject(){
//将java对象转换成JSON字符串
Teacher teacher=new Teacher();
teacher.setName("aa");
teacher.setSex("男");
teacher.setAge(28);
teacher.setBirth(new Date());
//JSON字符串
//{name:"aa",sex:"男",age:"28",birth:"...."}
//需要引入类库文件
System.out.println(JSONObject.fromObject(teacher));
}
{"age":28,"birth":{"date":28,"day":2,"hours":9,"minutes":32,"month":1,"seconds":5,"time":1330392725328,"timezoneOffset":-480,"year":112},"name":"aa","sex":"男"}
//将java集合转换成JSON字符串
@Test
public void fromVector(){
List<Teacher> teas=new ArrayList<Teacher>();
for(int i=0;i<3;i++){
Teacher teacher=new Teacher();
teacher.setName("aa");
teacher.setSex("男");
teacher.setAge(28);
teacher.setBirth(new Date());
teas.add(teacher);
}
//teas集合
System.out.println(JSONArray.fromObject(teas).toString());
}
[{"age":28,"birth":{"date":28,"day":2,"hours":9,"minutes":29,"month":1,"seconds":18,"time":1330392558562,"timezoneOffset":-480,"year":112},"name":"aa","sex":"男"},{"age":28,"birth":{"date":28,"day":2,"hours":9,"minutes":29,"month":1,"seconds":18,"time":1330392558562,"timezoneOffset":-480,"year":112},"name":"aa","sex":"男"},{"age":28,"birth":{"date":28,"day":2,"hours":9,"minutes":29,"month":1,"seconds":18,"time":1330392558562,"timezoneOffset":-480,"year":112},"name":"aa","sex":"男"}]
// JSON字符串转换为对象
@Test
public void fromJsonObject() {
// 第一步:定义成字符串
String teaObj = "{\"age\":28,\"birth\":{\"date\":28,\"day\":2,\"hours\":9,\"minutes\":20,\"month\":1,\"seconds\":25,\"time\":1330392025906,\"timezoneOffset\":-480,\"year\":112},\"name\":\"aa\",\"sex\":\"男\"}";
// 第二步:使用JsonObject.fromObject方法将其字符串转换为JsonObject对象
JSONObject jsonObj = JSONObject.fromObject(teaObj);
// 第三步:调用JsonObject的toBean的方法,将其转换成一个Object对象
Teacher teac = (Teacher) JSONObject.toBean(jsonObj,Teacher.class);
System.out.println(teac.toString());
}
Teacher [age=28, birth=Tue Feb 28 09:20:25CST 2012, name=aa, sex=男, stus=null]
// 将数组的字符串转换成集合对象
@SuppressWarnings("unchecked")
@Test
public void fromJsonArray() {
// 声明字符串
String teaArr = "[{\"age\":28,\"birth\":{\"date\":28,\"day\":2,\"hours\":9,\"minutes\":28,\"month\":1,\"seconds\":44,\"time\":1330392524093,\"timezoneOffset\":-480,\"year\":112},\"name\":\"aa\",\"sex\":\"男\"},{\"age\":28,\"birth\":{\"date\":28,\"day\":2,\"hours\":9,\"minutes\":28,\"month\":1,\"seconds\":44,\"time\":1330392524093,\"timezoneOffset\":-480,\"year\":112},\"name\":\"aa\",\"sex\":\"男\"},{\"age\":28,\"birth\":{\"date\":28,\"day\":2,\"hours\":9,\"minutes\":28,\"month\":1,\"seconds\":44,\"time\":1330392524093,\"timezoneOffset\":-480,\"year\":112},\"name\":\"aa\",\"sex\":\"男\"}]";
// 调用JsonArray.fromObject方法将其字符串转成一个JsonArray'对象
JSONArray jsonArra = JSONArray.fromObject(teaArr);
// 第三步:调用
List<Teacher> teas = (List<Teacher>) JSONArray.toCollection(jsonArra,
Teacher.class);
for (Teacher tea : teas) {
System.out.println(tea.toString());
}
}
Teacher [age=28, birth=Tue Feb28 09:28:44 CST 2012, name=aa, sex=男, stus=null]
Teacher [age=28, birth=Tue Feb28 09:28:44 CST 2012, name=aa, sex=男, stus=null]
Teacher [age=28, birth=Tue Feb 28 09:28:44CST 2012, name=aa, sex=男, stus=null]
//将java对象转换为json字符的案例
@Test
public void fromObject1(){
// 将java对象转换为JSON数据
Teacher teacher = new Teacher();
teacher.setName("老王");
teacher.setAge(23);
teacher.setBirth(new Date());
List<Student> seas = new ArrayList<Student>();
for (int i = 0; i < 3; i++) {
Student stu = new Student();
stu.setName("熊熊"+i);
seas.add(stu);
}
teacher.setStus(seas);
System.out.println(JSONObject.fromObject(teacher).toString());
}
{"age":23,"birth":{"date":28,"day":2,"hours":13,"minutes":50,"month":1,"seconds":22,"time":1330408222921,"timezoneOffset":-480,"year":112},"name":"老王","sex":"","stus":[{"name":"熊熊0"},{"name":"熊熊1"},{"name":"熊熊2"}]}