JSON几种转换方法
1、 首先创建一个web程序,
在domain包下创建Student 和 Teacher 类。 在Teacher.java中封装name,sex,age,birth属性,并实现 其构造器 set和get方法,以及toString()方法。
2、 在junit包下创建一个测试类 。 用来进行测试。
Demo.java
//第一个案例:将Java对象转换成Json字符串
@Test
public void fromObject(){
//将Java对象转换成Json字符串
Teacher teacher = new Teacher();
teacher.setName("redmary");
teacher.setSex("男");
teacher.setAge(21);
teacher.setBirth(new Date());
//json字符串
/*
* {name:"redarmy",sex:"男",age:21,birth:{对象}
* */
//需要引入类库文件
//调用JSONObject.fromObject(object)完成转换
System.out.println(JSONObject.fromObject(teacher).toString());
}
//第二个案例:将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("redmary");
teacher.setSex("男");
teacher.setAge(21);
teacher.setBirth(new Date());
teas.add(teacher);
}
//调用JSONArray.fromObject(object)完成转换
System.out.println(JSONArray.fromObject(teas).toString());
}
//第三个案例:将JSON字符串转换成对象
@Test
public void fromJsonObject(){
//第一步:定义出字符串
String teaObj = "{\"age\":21,\"birth\":{\"date\":28,\"day\":2,\"hours\":9,\"minutes\":31,\"month\":1,\"seconds\":38,\"time\":1330392698421,\"timezoneOffset\":-480,\"year\":112},\"name\":\"redmary\",\"sex\":\"男\"}";
//第二步:调用JSONObject.fromObject(teaObj)方法将其字符串转换成JsonObject对象
JSONObject jsonObj = JSONObject.fromObject(teaObj);
//第三步:调用JSONObject的toBean方法 ,将其转换成一个Object对象
Teacher teac = (Teacher) JSONObject.toBean(jsonObj,Teacher.class);
System.out.println(teac.toString());
}
//第四个案例:将数组的字符串转换成集合对象
@Test
public void fromJsonArray(){
//第一步:声明出字符串
String teaArr = "[{\"age\":21,\"birth\":{\"date\":28,\"day\":2,\"hours\":9,\"minutes\":31,\"month\":1,\"seconds\":38,\"time\":1330392698421,\"timezoneOffset\":-480,\"year\":112},\"name\":\"redmary\",\"sex\":\"男\"},{\"age\":21,\"birth\":{\"date\":28,\"day\":2,\"hours\":9,\"minutes\":31,\"month\":1,\"seconds\":38,\"time\":1330392698421,\"timezoneOffset\":-480,\"year\":112},\"name\":\"redmary\",\"sex\":\"男\"},{\"age\":21,\"birth\":{\"date\":28,\"day\":2,\"hours\":9,\"minutes\":31,\"month\":1,\"seconds\":38,\"time\":1330392698421,\"timezoneOffset\":-480,\"year\":112},\"name\":\"redmary\",\"sex\":\"男\"}]";
//第二步:调用JSONArray.fromObject(teaArr)方法将其字符串转换成一个JsonArray对象
JSONArray jsonArra = JSONArray.fromObject(teaArr);
//第三步:调用JSONArray的toCollection方法将其转换成指定泛型的List集合对象
List<Teacher> teas = (List<Teacher>) JSONArray.toCollection(jsonArra,Teacher.class);
for(Teacher tea:teas){
System.out.println(teas.toString());
}
}
//第五个案例:将Java对象转换成Json字符串
@Test
public void fromObject1(){
//将Java对象转换成Json字符串
Teacher teacher = new Teacher();
teacher.setName("redmary");
teacher.setSex("男");
teacher.setAge(21);
teacher.setBirth(new Date());
//json字符串
/*
* {name:"redarmy",sex:"男",age:21,birth:{对象}
* */
List<Student> stus = new ArrayList<Student>();
for(int i=0;i<3;i++){
Student stu = new Student();
stu.setName("pangli"+i);
stus.add(stu);
}
teacher.setStus(stus);
//需要引入类库文件
//调用JSONObject.fromObject(object)完成转换
System.out.println(JSONObject.fromObject(teacher).toString());
}