Json lib 教程
Json_lib可以方便的將Java對象轉成json格式的字符串,也可以將Java對象轉換成xml格式的文檔,同樣可以將Json字符串轉換成Java對象,或者將xml字符串轉換成Java對象。
官網:http://json-lib.sourceforge.net/
JSON在線轉換:http://json.parser.online.fr/
JSON教程:http://www.json.org/json-zh.html
官網上說明了json_lib還需要依賴的Jar包有:
JAR |
網址 |
jakarta commons-collections 3.2.1 |
http://commons.apache.org/collections/download_collections.cgi |
jakarta commons-logging 1.1.1 |
|
ezmorph 1.0.6 |
注意這里如果jakarta commons-lang 使用3.1版本的會報:Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
所以這里還是使用2.6的吧
MyBean.java:
package com.iflytek.json; import net.sf.json.JSONFunction; /** * @author xudongwang 2012-1-15 * * Email:xdwangiflytek@gmail.com */ public class MyBean { private String name = "json"; private int pojoId = 1; private char[] options = new char[] { 'a', 'f' }; private String func1 = "function(i){ return this.options[i]; }"; private JSONFunction func2 = new JSONFunction(new String[] { "i" }, "return this.options[i];"); public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPojoId() { return pojoId; } public void setPojoId(int pojoId) { this.pojoId = pojoId; } public char[] getOptions() { return options; } public void setOptions(char[] options) { this.options = options; } public String getFunc1() { return func1; } public void setFunc1(String func1) { this.func1 = func1; } public JSONFunction getFunc2() { return func2; } public void setFunc2(JSONFunction func2) { this.func2 = func2; } }JsonlibTest.java :
package com.iflytek.json; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.beanutils.PropertyUtils; /** * @author xudongwang 2012-1-15 * * Email:xdwangiflytek@gmail.com */ public class JsonlibTest { // JSONArray是將一個Java對象轉換成json的Array格式,如['xdwang', 22] private JSONArray jsonArray = null; // JSONObject是將Java對象轉換成一個json的Object形式,如{name:'xdwang', age: 22} private JSONObject jsonObject = null; public static void main(String[] args) { JsonlibTest json = new JsonlibTest(); json.ArrayToJSON(); json.ListToJSON(); json.MapsToJSON(); json.BeanToJSON(); json.JSONToBean(); } /** * 數組轉JSON操作 */ public void ArrayToJSON() { boolean[] boolArray = new boolean[] { true, false, true }; jsonArray = JSONArray.fromObject(boolArray); System.out.println("數組轉JSON操作:" + jsonArray); } /** * 集合轉JSON操作 */ public void ListToJSON() { List<String> list = new ArrayList<String>(); list.add("first"); list.add("second"); jsonArray = JSONArray.fromObject(list); System.out.println("集合轉JSON操作:" + jsonArray); } /** * Maps轉JSON操作 */ public void MapsToJSON() { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); jsonObject = JSONObject.fromObject(map); System.out.println("Maps轉JSON操作:" + jsonObject); } /** * Bean轉JSON操作 */ public void BeanToJSON() { jsonObject = JSONObject.fromObject(new MyBean()); System.out.println("Bean轉JSON操作:" + jsonObject); } /** * JSON轉Bean操作 */ public void JSONToBean() { try { String json = "{\"func1\":function(i){ return this.options[i]; },\"func2\":function(i){ return this.options[i]; },\"name\":\"json\",\"options\":[\"a\",\"f\"],\"pojoId\":1}"; jsonObject = JSONObject.fromObject(json); Object bean = JSONObject.toBean(jsonObject); System.out.println("jsonStr:" + json); System.out.println("name:" + jsonObject.get("name")); System.out.println("name:" + PropertyUtils.getProperty(bean, "name")); System.out.println("pojoId:" + jsonObject.get("pojoId")); System.out.println("pojoId:" + PropertyUtils.getProperty(bean, "pojoId")); System.out.println("options:" + jsonObject.get("options")); System.out.println("options:" + PropertyUtils.getProperty(bean, "options")); System.out.println("func1:" + jsonObject.get("func1")); System.out.println("func1:" + PropertyUtils.getProperty(bean, "func1")); System.out.println("func2:" + jsonObject.get("func2")); System.out.println("func2:" + PropertyUtils.getProperty(bean, "func2")); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }轉自:http://xdwangiflytek.iteye.com/blog/1345380
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!