Json-lib使用——JSONObject與JSONArray

1.從Object到String
 要先用Object對象構造一個JSONObject或者JSONArray對象,而後調用它的toString()方法便可java

(1)示例一json

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)示例一 ide

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)示例二spa

複製代碼
1 String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]";
2 JSONArray array=JSONArray.fromObject(json);
3 for(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到Beancode

(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的數組blog

複製代碼
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);
4         for(Product p:products){
5             System.out.println(p.getName()+" "+p.getPrice());
6         }
複製代碼

自定義封裝JSON操做的類字符串

複製代碼
 1 package com.util;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import net.sf.json.JSONArray;
 7 import net.sf.json.JSONObject;
 8 
 9 public class JsonHelper {
10     //從普通的Bean轉換爲字符串
11 public static String getJson(Object o){
12 JSONObject jo=JSONObject.fromObject(o); 13 return jo.toString(); 14 } 15 //從Java的列表轉換爲字符串
16 public static String getJson(List list){
17 JSONArray ja=JSONArray.fromObject(list); 18 return ja.toString(); 19 } 20 //從Java對象數組轉換爲字符串
21 public static String getJson(Object[] arry){
22 JSONArray ja=JSONArray.fromObject(arry); 23 return ja.toString(); 24 } 25 //從json格式的字符串轉換爲Map對象
26 public static Map getObject(String s){
27 return JSONObject.fromObject(s); 28 } 29 //從json格式的字符串轉換爲List數組
30 public static List getArray(String s){
31 return JSONArray.fromObject(s); 32 } 33 //從json格式的字符串轉換爲某個Bean
34 public static Object getObject(String s,Class cls){
35 JSONObject jo=JSONObject.fromObject(s); 36 return JSONObject.toBean(jo, cls); 37 } 38 //從json格式的字符串轉換爲某類對象的數組
39 public static Object getArray(String s,Class cls){
40 JSONArray ja=JSONArray.fromObject(s); 41 return JSONArray.toArray(ja, cls); 42 } 43 }
相關文章
相關標籤/搜索