json-lib使用——JSONObject與JSONArray

  ps:看這篇博客以前首先要引入工具包json-lib-2.2.2-jdk15.jarjava

  資源連接:百度雲:連接:https://pan.baidu.com/s/1o9k7PSu 密碼:00ljjson

 

1、從Object到String數組

  要先用Object對象構造一個JSONObject或者JSONArray對象,而後調用它的toString()方法便可ide

  (1)示例一工具

Book book=new Book();
book.setName("Java");
book.setPrice(52.3f);
JSONObject object=JSONObject.fromObject(book);
System.out.println(object.toString());
View Code

  (2)示例二spa

Book book=new Book();
book.setName("Java");
book.setPrice(52.3f);
        
Book book2=new Book();
book2.setName("C");
book2.setPrice(42.3f);
List list=new ArrayList();
list.add(book);
list.add(book2);
JSONArray arry=JSONArray.fromObject(list);
System.out.println(arry.toString());
//結果以下:
[{"name":"Java","price":52.3},{"name":"C","price":42.3}]
View Code

 

2、從String到Objectcode

  要先用String對象構造一個JSONObject或者JSONArray對象對象

  (1)示例一blog

String json="{name:'Java',price:52.3}";    
JSONObject object=JSONObject.fromObject(json);
System.out.println(object.get("name")+" "+object.get("price"));
View Code

  (2)示例二資源

String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]";
JSONArray array=JSONArray.fromObject(json);
for(int i=0;i<array.size();i++){
    Map o=(Map)array.get(i);
    System.out.println(o.get("name")+" "+o.get("price"));
}
View Code

 

3、從String到Bean

  (1)單個Bean對象

String json="{name:'Java',price:52.3}";
JSONObject object=JSONObject.fromObject(json);
Product product=(Product)JSONObject.toBean(object,Product.class);
System.out.println(product.getName()+" "+product.getPrice());
View Code

  (2)Bean的數組

String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]";
JSONArray array=JSONArray.fromObject(json);
Product[] products=(Product[]) JSONArray.toArray(array,Product.class);
for(Product p:products){
       System.out.println(p.getName()+" "+p.getPrice());
 }
View Code

 

3、自定義封裝JSON操做的類

package com.util;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonHelper {
    //從普通的Bean轉換爲字符串
    public static String getJson(Object o){
        JSONObject jo=JSONObject.fromObject(o);
        return jo.toString();
    }
    //從Java的列表轉換爲字符串
    public static String getJson(List list){
        JSONArray ja=JSONArray.fromObject(list);
        return ja.toString();
    }
    //從Java對象數組轉換爲字符串
    public static String getJson(Object[] arry){
        JSONArray ja=JSONArray.fromObject(arry);
        return ja.toString();
    }
    //從json格式的字符串轉換爲Map對象
    public static Map getObject(String s){
        return     JSONObject.fromObject(s);
    }
    //從json格式的字符串轉換爲List數組
    public static List getArray(String s){
        return JSONArray.fromObject(s);
    }
    //從json格式的字符串轉換爲某個Bean
    public static Object getObject(String s,Class cls){
        JSONObject jo=JSONObject.fromObject(s);
        return JSONObject.toBean(jo, cls);
    }
    //從json格式的字符串轉換爲某類對象的數組
    public static Object getArray(String s,Class cls){
        JSONArray ja=JSONArray.fromObject(s);
        return JSONArray.toArray(ja, cls);
    }
}
View Code
相關文章
相關標籤/搜索