JSON Format

转载http://blog.toright.com/?p=397
JSON Format
JSON為輕量級的資料表示格式,比起 XML 鬆散許多且不需要定義描述檔,JSON網站 http://json.org/

Json-lib API
Json-lib 為在眾多處理 Jsob Format API 中實作功能比較多的一個套件,可由下列網址取得。

http://json-lib.sourceforge.net/ 此外,Json-lib還需要以下 Library

jakarta commons-lang 2.4
jakarta commons-beanutils 1.7.0
jakarta commons-collections 3.2
jakarta commons-logging 1.1.1
ezmorph 1.0.6
JSON 與 POJO 轉換實作
設計兩個Model

Java代码 
Model.java  
 
private String title;  
    private int num;  
    private Integer sort;  
    private Date date;  
    private String[] strArray;  
    private SubModel subModel;  
SubModel.java  
 
private String name;  
Marshal 實作  
Object2JSON.java  
 
import java.util.Date;  
 
import net.sf.json.JSONObject;  
 
public class Object2JSON {  
 
    public static void main(String[] args) {  
        //產生Model  
        Model model = new Model();  
        model.setTitle("Master MOdel");  
        model.setNum(99);  
        model.setSort(10);  
        model.setDate(new Date());  
        model.setStrArray(new String[]{"str1","str2"});  
        SubModel subModel = new SubModel();  
        subModel.setName("MySubModel");  
        model.setSubModel(subModel);  
        //轉換為JSON  
        String json = JSONObject.fromObject(model).toString();  
        //顯示XML  
        System.out.println(json);  
    }  
 
}  
Unmarshal 實作  
JSON2Object.java  
 
import net.sf.json.JSONObject;  
 
public class JSON2Object {  
 
    public static void main(String[] args) {  
        String json = "{\"date\":{\"date\":27,\"day\":2,\"hours\":15,\"minutes\":30,\"month\":9,\"seconds\":35,\"time\":1256628635421,\"timezoneOffset\":-480,\"year\":109},\"num\":99,\"sort\":10,\"strArray\":[\"str1\",\"str2\"],\"subModel\":{\"name\":\"MySubModel\"},\"title\":\"Master MOdel\"}";  
        //轉換JavaBean  
        Model model = (Model)JSONObject.toBean( JSONObject.fromObject( json ), Model.class );  
        //顯示內容  
        System.out.println("Title:"+model.getTitle());  
        System.out.println("Num:"+model.getNum());  
        System.out.println("Sort:"+model.getSort());  
        System.out.println("Date:"+model.getDate());  
        System.out.println("SubModel:"+model.getSubModel());  
        System.out.println("SubModel[Name]:"+model.getSubModel().getName());  
    }  
 


Model.java

private String title;
private int num;
private Integer sort;
private Date date;
private String[] strArray;
private SubModel subModel;
SubModel.java

private String name;
Marshal 實作
Object2JSON.java

import java.util.Date;

import net.sf.json.JSONObject;

public class Object2JSON {

public static void main(String[] args) {
//產生Model
Model model = new Model();
model.setTitle("Master MOdel");
model.setNum(99);
model.setSort(10);
model.setDate(new Date());
model.setStrArray(new String[]{"str1","str2"});
SubModel subModel = new SubModel();
subModel.setName("MySubModel");
model.setSubModel(subModel);
//轉換為JSON
String json = JSONObject.fromObject(model).toString();
//顯示XML
System.out.println(json);
}

}
Unmarshal 實作
JSON2Object.java

import net.sf.json.JSONObject;

public class JSON2Object {

public static void main(String[] args) {
String json = "{\"date\":{\"date\":27,\"day\":2,\"hours\":15,\"minutes\":30,\"month\":9,\"seconds\":35,\"time\":1256628635421,\"timezoneOffset\":-480,\"year\":109},\"num\":99,\"sort\":10,\"strArray\":[\"str1\",\"str2\"],\"subModel\":{\"name\":\"MySubModel\"},\"title\":\"Master MOdel\"}";
//轉換JavaBean
Model model = (Model)JSONObject.toBean( JSONObject.fromObject( json ), Model.class );
//顯示內容
System.out.println("Title:"+model.getTitle());
System.out.println("Num:"+model.getNum());
System.out.println("Sort:"+model.getSort());
System.out.println("Date:"+model.getDate());
System.out.println("SubModel:"+model.getSubModel());
System.out.println("SubModel[Name]:"+model.getSubModel().getName());
}

}Json-lib 結論
過測試之後發現 Json 對 Array 是支援的,但是對於 List 與 Hashtable 的 Unmarshal 卻是不支援。假設 Json 未來成為 Web Service 重要的傳輸格式,那麼實作 Json 更高階的 Unmarshal 是絕對必要的工作。

你可能感兴趣的:(JSON Format)