JSON(二)


//第一个案例: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"}]}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值