Json转换
http://jc-dreaming.iteye.com/blog/1007157
import java.util.Collection; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * JSON和JAVA的POJO的相互转换 * * @author ptah 2009-10-16 JSONUtil.java */ public final class JSONUtil { // 将String转换成JSON public static String string2json(String key, String value) { JSONObject object = new JSONObject(); object.put(key, value); return object.toString(); } // 将JSON转换成数组,其中valueClz为数组中存放的对象的Class public static Object json2Array(String json, Class valueClz) { JSONArray jsonArray = JSONArray.fromObject(json); return JSONArray.toArray(jsonArray, valueClz); } // 将Collection转换成JSON public static String collection2json(Object object) { JSONArray jsonArray = JSONArray.fromObject(object); return jsonArray.toString(); } // 将JSON转换成Collection,其中collectionClz为Collection具体子类的Class, // valueClz为Collection中存放的对象的Class public static Collection json2Collection(String json, Class collectionClz, Class valueClz) { JSONArray jsonArray = JSONArray.fromObject(json); return JSONArray.toCollection(jsonArray, valueClz); } // 将数组转换成JSON public static String array2json(Object object) { JSONArray jsonArray = JSONArray.fromObject(object); return jsonArray.toString(); } // 将Map转换成JSON public static String map2json(Object object) { JSONObject jsonObject = JSONObject.fromObject(object); return jsonObject.toString(); } // 将JSON转换成Map,其中valueClz为Map中value的Class,keyArray为Map的key public static Map json2Map(Object[] keyArray, String json, Class valueClz) { JSONObject jsonObject = JSONObject.fromObject(json); Map classMap = new HashMap(); for (int i = 0; i < keyArray.length; i++) { classMap.put(keyArray[i], valueClz); } return (Map) JSONObject.toBean(jsonObject, Map.class, classMap); } // 将POJO转换成JSON public static String bean2json(Object object) { JSONObject jsonObject = JSONObject.fromObject(object); return jsonObject.toString(); } // 将JSON转换成POJO,其中beanClz为POJO的Class public static Object json2Object(String json, Class beanClz) { return JSONObject.toBean(JSONObject.fromObject(json), beanClz); } // 将JSON转换成String public static String json2String(String json, String key) { JSONObject jsonObject = JSONObject.fromObject(json); return jsonObject.get(key).toString(); } } import java.util.Collection; import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * JSON和JAVA的POJO的相互转换 * * @author ptah 2009-10-16 JSONUtil.java */ public final class JSONUtil { // 将String转换成JSON public static String string2json(String key, String value) { JSONObject object = new JSONObject(); object.put(key, value); return object.toString(); } // 将JSON转换成数组,其中valueClz为数组中存放的对象的Class public static Object json2Array(String json, Class valueClz) { JSONArray jsonArray = JSONArray.fromObject(json); return JSONArray.toArray(jsonArray, valueClz); } // 将Collection转换成JSON public static String collection2json(Object object) { JSONArray jsonArray = JSONArray.fromObject(object); return jsonArray.toString(); } // 将JSON转换成Collection,其中collectionClz为Collection具体子类的Class, // valueClz为Collection中存放的对象的Class public static Collection json2Collection(String json, Class collectionClz, Class valueClz) { JSONArray jsonArray = JSONArray.fromObject(json); return JSONArray.toCollection(jsonArray, valueClz); } // 将数组转换成JSON public static String array2json(Object object) { JSONArray jsonArray = JSONArray.fromObject(object); return jsonArray.toString(); } // 将Map转换成JSON public static String map2json(Object object) { JSONObject jsonObject = JSONObject.fromObject(object); return jsonObject.toString(); } // 将JSON转换成Map,其中valueClz为Map中value的Class,keyArray为Map的key public static Map json2Map(Object[] keyArray, String json, Class valueClz) { JSONObject jsonObject = JSONObject.fromObject(json); Map classMap = new HashMap(); for (int i = 0; i < keyArray.length; i++) { classMap.put(keyArray[i], valueClz); } return (Map) JSONObject.toBean(jsonObject, Map.class, classMap); } // 将POJO转换成JSON public static String bean2json(Object object) { JSONObject jsonObject = JSONObject.fromObject(object); return jsonObject.toString(); } // 将JSON转换成POJO,其中beanClz为POJO的Class public static Object json2Object(String json, Class beanClz) { return JSONObject.toBean(JSONObject.fromObject(json), beanClz); } // 将JSON转换成String public static String json2String(String json, String key) { JSONObject jsonObject = JSONObject.fromObject(json); return jsonObject.get(key).toString(); } }
转载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
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 是絕對必要的工作。 */
无为而治