Json体验之——Json-lib使用浅谈(二)

这一部分主要讲解使用json-lib包将java数据类型转换成json的数据类型,也就是java——>json。

 


 

首先介绍JSONSerializer,它使用i简单干净的接口方式将任何java与json直接进行转换,java——>json,使用JSONSerializer.toJSON(),json——>java 使用toJava()。

1     // 将string转换成json
2     String s1 = new String("{1:1}");
3     String s2 = new String("[1,1]");
4     JSONObject json1 = (JSONObject) JSONSerializer.toJSON(s1);
5     System.out.println("——用JSONSerializer将string转换json——" + json1);
6     JSONArray json2 = (JSONArray) JSONSerializer.toJSON(s2);
7     System.out.println("——用JSONSerializer将string转换jsonArray——" + json2);

 显示结果

1 ——用JSONSerializer将string转换json——{"1":1}
2 ——用JSONSerializer将string转换jsonArray——[1,1]

 

将java数组和集合转换成json

 1     // Working with arrays and collections,——>json
 2     boolean[] booleanArray = new boolean[] { true, false, true };
 3     JSONArray fromObject = JSONArray.fromObject(booleanArray);
 4     System.out.println(fromObject);
 5 
 6     List list = new ArrayList();
 7     list.add("first");
 8     list.add("second");
 9     JSONArray fromObject2 = JSONArray.fromObject(list);
10     System.out.println(fromObject2);    

结果

1 [true,false,true]
2 ["first","second"]

将beans和map转换成json

 1       // Working with objects
 2         // From Beans & Maps toJSON
 3         Map map = new HashMap();
 4         map.put("name", "json");
 5         map.put("bool", Boolean.TRUE);
 6         map.put("int", new Integer(1));
 7         map.put("arr", new String[] { "a", "b" });
 8         map.put("func", "function(i){ return this.arr[i]; }");
 9 
10         JSONObject jsonObject2 = JSONObject.fromObject(map);
11         System.out.println("——map转换json——\n" + jsonObject2);

结果

1 ——map转换json——
2 {"arr":["a","b"],"int":1,"name":"json","func":function(i){ return this.arr[i]; },"bool":true}

beans转换先自定义对象Mybean,

 1 public class MyBean {
 2 
 3     private String name = "json";
 4     private int pojoId = 1;
 5     private char[] options = new char[] { 'a', 'f' };
 6     private String func1 = "function(i){ return this.options[i]; }";
 7     private JSONFunction func2 = new JSONFunction(new String[] { "i" },
 8             "return this.options[i];");
 9      //自定义类型
10     private MyBean2 mybean2 = new MyBean2("1", "mybean");
11 
12     public String getName() {
13         return name;
14     }
15     public void setName(String name) {
16         this.name = name;
17     }
18     public int getPojoId() {
19         return pojoId;
20     }
21     public void setPojoId(int pojoId) {
22         this.pojoId = pojoId;
23     }
24     public char[] getOptions() {
25         return options;
26     }
27     public void setOptions(char[] options) {
28         this.options = options;
29     }
30     public String getFunc1() {
31         return func1;
32     }
33     public void setFunc1(String func1) {
34         this.func1 = func1;
35     }
36     public JSONFunction getFunc2() {
37         return func2;
38     }
39     public void setFunc2(JSONFunction func2) {
40         this.func2 = func2;
41     }
42     public MyBean2 getMybean2() {
43         return mybean2;
44     }
45     public void setMybean2(MyBean2 mybean2) {
46         this.mybean2 = mybean2;
47     }
48 }

Mybean中包含自定义对象Mybean2

public class MyBean2 {
    private String id;
    private String name;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public MyBean2(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public MyBean2() {
        super();
    }
}

将beans转换为json

1     // 自定义MyBean对象中包含MyBean2对象,所以bean——>Json,无论是否自定义对象,都可以转换json格式
2         JSONObject jsonObject3 = JSONObject.fromObject(new MyBean());
3         System.out.println("——beans转换json——\n" + jsonObject3);

结果

1 ——beans转换json——
2 {"name":"json","mybean2":{"id":"1","name":"mybean"},"pojoId":1,"func1":function(i){ return this.options[i]; },"func2":function(i){ return this.options[i]; },"options":["a","f"]}

 


 

总结:将java对象转换成json,比较简单,若是beans或maps用JSONObject.fromObject() ,若是array或collections,用JSONArray.fromObject()。

转载于:https://www.cnblogs.com/R-blog/p/4176217.html

你可能感兴趣的:(json,java)