Json-lib使用――JSONObject与JSONArray

1.从Object到String
要先用Object对象构造一个JSONObject或者JSONArray对象,然后调用它的toString()方法即可

(1)示例一

复制代码
1 Book book=new Book();
2 book.setName("Java");
3 book.setPrice(52.3f);
4 JSONObject object=JSONObject.fromObject(book);
5 System.out.println(object.toString());
复制代码

(2)示例二

复制代码
 1         Book book=new Book();
2         book.setName("Java");
3         book.setPrice(52.3f);
4        
5         Book book2=new Book();
6         book2.setName("C");
7         book2.setPrice(42.3f);
8         List list=new ArrayList();
9         list.add(book);
10         list.add(book2);
11         JSONArray arry=JSONArray.fromObject(list);
12         System.out.println(arry.toString());
13//结果如下:
14 [{"name":"Java","price":52.3},{"name":"C","price":42.3}]
复制代码

2.从String到Object
要先用String对象构造一个JSONObject或者JSONArray对象
(1)示例一

复制代码
1 String json="{name:'Java',price:52.3}";    
2 JSONObject object=JSONObject.fromObject(json);
3 System.out.println(object.get("name")+" "+object.get("price"));
复制代码

(2)示例二

复制代码
1 String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]";
2 JSONArray array=JSONArray.fromObject(json);
3for(int i=0;i<array.size();i++){
4     Map o=(Map)array.get(i);
5     System.out.println(o.get("name")+" "+o.get("price"));
6 }
复制代码

3.从String到Bean

(1)单个Bean对象

复制代码
1         String json="{name:'Java',price:52.3}";
2         JSONObject object=JSONObject.fromObject(json);
3         Product product=(Product)JSONObject.toBean(object,Product.class);
4         System.out.println(product.getName()+" "+product.getPrice());
复制代码

(2).Bean的数组

复制代码
1         String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]";
2         JSONArray array=JSONArray.fromObject(json);
3         Product[] products=(Product[]) JSONArray.toArray(array,Product.class);
4for(Product p:products){
5             System.out.println(p.getName()+" "+p.getPrice());
6         }
复制代码

自定义封装JSON操作的类

复制代码
 1package com.util;
2
3import java.util.List;
4import java.util.Map;
5
6import net.sf.json.JSONArray;
7import net.sf.json.JSONObject;
8
9publicclass JsonHelper {
10//从普通的Bean转换为字符串
11publicstatic String getJson(Object o){

12         JSONObject jo=JSONObject.fromObject(o);
13return jo.toString();
14     }
15//从Java的列表转换为字符串
16publicstatic String getJson(List list){

17         JSONArray ja=JSONArray.fromObject(list);
18return ja.toString();
19     }
20//从Java对象数组转换为字符串
21publicstatic String getJson(Object[] arry){

22         JSONArray ja=JSONArray.fromObject(arry);
23return ja.toString();
24     }
25//从json格式的字符串转换为Map对象
26publicstatic Map getObject(String s){

27return     JSONObject.fromObject(s);
28     }
29//从json格式的字符串转换为List数组
30publicstatic List getArray(String s){

31return JSONArray.fromObject(s);
32     }
33//从json格式的字符串转换为某个Bean
34publicstatic Object getObject(String s,Class cls){

35         JSONObject jo=JSONObject.fromObject(s);
36return JSONObject.toBean(jo, cls);
37     }
38//从json格式的字符串转换为某类对象的数组
39publicstatic Object getArray(String s,Class cls){

40         JSONArray ja=JSONArray.fromObject(s);
41return JSONArray.toArray(ja, cls);
42     }
43 }


你可能感兴趣的:(JSONObject,JsonArray)