Java对JSON的简单操作

来源:互联网 发布:python 分割文件 编辑:程序博客网 时间:2024/06/05 16:55

JSONArray和JSONObject

基本用法

package com.cloud.test;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

publicclass Demo1 {

   //创建JSONObject对象

   publicstatic JSONObject createJSONObject(){

      JSONObject jsonObject = new JSONObject();

      jsonObject.put("username","Spring");

      jsonObject.put("sex","");

      jsonObject.put("QQ","123456");

      return jsonObject;

   }

   @SuppressWarnings("static-access")

   publicstatic void main(String[] args)throws Exception {

      //创建一个JSONObject对象

      JSONObject jsonObject = Demo1.createJSONObject();

      //输出jsonObject{"sex":"","username":"Spring","QQ":"123456"}

      System.out.println(jsonObject);

      //判断jsonObject的对象类

      System.out.println("数组?"+jsonObject.isArray()+";为空?"+

                    jsonObject.isEmpty()+";isNullObject?"+

                    jsonObject.isNullObject()+";");

      //追加元素属性

      jsonObject.put("address","安徽合肥");

      System.out.println(jsonObject);

     

      //返回一个JSONArray对象

        JSONArray jsonArray = new JSONArray();

        jsonArray.add(0, "Value0");

        jsonArray.add(1, "Value1");

        jsonObject.element("jsonArray", jsonArray);

       

        //jsonObject后面住家一个jsonArray

        JSONArray array = jsonObject.getJSONArray("jsonArray");

        System.out.println(jsonObject);

        System.out.println("返回一个JSONArray对象:" + array);

       

        //根据key返回字符串

        String username = jsonObject.getString("username");

        System.out.println("username:"+username);

       

        //字符串转换为jsonObject

        String temp = jsonObject.toString();

        System.out.println(temp);

        JSONObject object = jsonObject.fromObject(temp);

        System.out.println("QQ:"+object.get("QQ"));

   }

}

案例

package com.cloud.test;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

publicclass Demo2 {

   publicstatic void main(String[] args) {

      JSONObject jsonObject0 = new JSONObject();

      JSONObject jsonObject1 = new JSONObject();

      JSONObject jsonObject2 = new JSONObject();

      JSONObject jsonObject3 = new JSONObject();

      JSONArray jsonArray = new JSONArray();

     

      //jsonObject0

      jsonObject0.put("name0","Spring");

      jsonObject0.put("sex0","man");

      System.out.println("jsonObject0:"+jsonObject0);

     

      //jsonObject1

      jsonObject1.put("name1","summer");

      jsonObject1.put("sex1","woman");

      System.out.println("jsonObject1"+jsonObject1);

     

      //jsonObject2

      jsonObject2.put("item0", jsonObject0);

      jsonObject2.put("item1", jsonObject1);

      System.out.println("jsonObject2"+jsonObject2);

     

      //jsonObject3

      jsonObject3.put("jo31", jsonObject2);

      jsonObject3.put("jo32", jsonObject3);

      System.out.println("jsonObject3"+jsonObject3);

     

      //JSONArray中添加JSONObject对象。发现JSONArrayJSONObject的区别就是JSONArrayJSONObject多中括号[]

      jsonArray.add(jsonObject1);

      System.out.println(jsonArray);

     

      JSONObject jsonObject4 = new JSONObject();

      jsonObject4.element("weater", jsonArray);

      System.out.println("jsonObject4:"+jsonObject4);

   }

}

FromObject和toBean

fromObject

package com.cloud.test;

import java.util.HashMap;

import java.util.Map;

import net.sf.json.JSONObject;

publicclass fo {

   @SuppressWarnings("rawtypes")

   publicstatic void main(String[] args) {

      Map map = new HashMap();

      map.put("spring","spring1");

      map.put("summer","summer1");

      map.put("autumn","autumn1");

      JSONObject json = JSONObject.fromObject(map);

      //{"autumn":"autumn1","summer":"summer1","spring":"spring1"}

      System.out.println(json);

   }

}

toBean

student.java实体类

package com.cloud.test;

publicclass Student{

   privateint id;

   private Stringname;

   privateint age;

   publicint getId() {

      returnid;

   }

   publicvoid setId(int id) {

      this.id = id;

   }

   public String getName() {

      returnname;

   }

   publicvoid setName(String name) {

      this.name = name;

   }

   publicint getAge() {

      returnage;

   }

   publicvoid setAge(int age) {

      this.age = age;

   }

   public String toString(){

      returnthis.id+";"+this.name+";"+this.age;

   }

}

tb.java

package com.cloud.test;

import net.sf.json.JSONObject;

publicclass tb {

   publicstatic void main(String[] args) {

      String json = "{id:'101',name:'你好',age:'22'}";

      Student stu = new Student();

      JSONObject jsonObject = JSONObject.fromObject(json);

      stu = (Student) JSONObject.toBean(jsonObject, Student.class);

      //101;你好;22

      System.out.println(stu);

     

      String json1 = "{id:'101',name:'张三'}";

        Student stu1 = new Student();

        JSONObject obj = JSONObject.fromObject(json1);

        System.out.println("obj:"+obj);

        stu1 = (Student)JSONObject.toBean(obj, Student.class);

        //101;张三;0

        System.out.println(stu1);

       

        String json2 = "{id:'101',age:'22'}";

        Student stu2 = new Student();

        JSONObject obj1 = JSONObject.fromObject(json2);

        stu2 = (Student)JSONObject.toBean(obj1, Student.class);

        //101;null;22

        System.out.println(stu2);

       

        String json3 = "{id:'101',name:'张三',age:'nn'}";

        Student stu3 = new Student();

        JSONObject obj2 = JSONObject.fromObject(json3);

        stu3 = (Student)JSONObject.toBean(obj2, Student.class);

        //101;张三;0

        System.out.println(stu3);

       

        String json4 = "{id:'101',name:'张三',age:'22',sex:''}";

        Student stu4 = new Student();

        JSONObject obj3 = JSONObject.fromObject(json4);

        stu4 = (Student)JSONObject.toBean(obj3, Student.class);

        //101;张三;22

        System.out.println(stu4);

   }

}

0 0