Json lib 教程
<p class="MsoNormal"><span lang="EN-US">Json_lib</span><span>可以方便的将</span><span lang="EN-US">Java</span><span>对象转成</span><span lang="EN-US">json</span><span>格式的字符串,也可以将</span><span lang="EN-US">Java</span><span>对象转换成</span><span lang="EN-US">xml</span><span>格式的文档,同样可以将</span><span lang="EN-US">Json</span><span>字符串转换成</span><span lang="EN-US">Java</span><span>对象,或者将</span><span lang="EN-US">xml</span><span>字符串转换成</span><span lang="EN-US">Java</span><span>对象。</span></p> <p class="MsoNormal"><span lang="EN-US"> </span></p> <p class="MsoNormal"><span>官网:</span><span lang="EN-US"><a href="/misc/goto?guid=4959499360114181159">http://json-lib.sourceforge.net/</a></span></p> <p class="MsoNormal"><span lang="EN-US">JSON</span><span>在线转换:</span><span lang="EN-US"><a href="/misc/goto?guid=4959500829247316326">http://json.parser.online.fr/</a></span></p> <p class="MsoNormal"><span lang="EN-US">JSON</span><span>教程:</span><span lang="EN-US"><a href="/misc/goto?guid=4959500829331297171">http://www.json.org/json-zh.html</a></span></p> <p class="MsoNormal"><span>官网上说明了</span><span lang="EN-US">json_lib</span><span>还需要依赖的</span><span lang="EN-US">Jar</span><span>包有:</span></p> <table style="border-bottom:medium none;border-left:medium none;width:466.1pt;border-collapse:collapse;border-top:medium none;border-right:medium none;" class="MsoTableGrid" border="1" cellspacing="0" cellpadding="0" width="621"> <tbody> <tr> <td style="padding-bottom:0cm;padding-left:5.4pt;width:161.35pt;padding-right:5.4pt;padding-top:0cm;" valign="top" width="215"> <p style="text-align:center;" class="MsoNormal" align="center"><strong><span lang="EN-US">JAR</span></strong></p> </td> <td style="border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:304.75pt;padding-right:5.4pt;padding-top:0cm;" valign="top" width="406"> <p style="text-align:center;" class="MsoNormal" align="center"><strong><span>网址</span></strong></p> </td> </tr> <tr> <td style="padding-bottom:0cm;padding-left:5.4pt;width:161.35pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="215"> <p class="MsoNormal"><a name="OLE_LINK8"></a><a name="OLE_LINK7"><span lang="EN-US">jakarta commons-lang 2.5 </span></a></p> </td> <td style="border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:304.75pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="406"> <p class="MsoNormal"><span lang="EN-US"><a href="/misc/goto?guid=4958184198898492184">http://commons.apache.org/lang/download_lang.cgi</a></span></p> </td> </tr> <tr> <td style="padding-bottom:0cm;padding-left:5.4pt;width:161.35pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="215"> <p class="MsoNormal"><a name="OLE_LINK2"></a><a name="OLE_LINK1"><span lang="EN-US">jakarta commons-beanutils 1.8.0 </span></a></p> </td> <td style="border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:304.75pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="406"> <p class="MsoNormal"><span lang="EN-US"><a href="/misc/goto?guid=4959500829442342809">http://commons.apache.org/beanutils/download_beanutils.cgi</a></span></p> </td> </tr> <tr> <td style="padding-bottom:0cm;padding-left:5.4pt;width:161.35pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="215"> <p class="MsoNormal"><span lang="EN-US">jakarta commons-<a name="OLE_LINK6"></a><a name="OLE_LINK5">collections </a>3.2.1</span></p> </td> <td style="border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:304.75pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="406"> <p class="MsoNormal"><span lang="EN-US"><a href="/misc/goto?guid=4959500829520695139">http://commons.apache.org/collections/download_collections.cgi</a></span></p> </td> </tr> <tr> <td style="padding-bottom:0cm;padding-left:5.4pt;width:161.35pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="215"> <p class="MsoNormal"><span lang="EN-US">jakarta commons-logging 1.1.1</span></p> </td> <td style="border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:304.75pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="406"> <p class="MsoNormal"><span lang="EN-US"><a href="/misc/goto?guid=4959500829604154371">http://commons.apache.org/logging/download_logging.cgi</a></span></p> </td> </tr> <tr> <td style="padding-bottom:0cm;padding-left:5.4pt;width:161.35pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="215"> <p class="MsoNormal"><a name="OLE_LINK4"></a><a name="OLE_LINK3"><span lang="EN-US">ezmorph </span></a><span lang="EN-US">1.0.6</span></p> </td> <td style="border-left:medium none;padding-bottom:0cm;padding-left:5.4pt;width:304.75pt;padding-right:5.4pt;border-top:medium none;padding-top:0cm;" valign="top" width="406"> <p class="MsoNormal"><span lang="EN-US"><a href="/misc/goto?guid=4959500829678349287">http://ezmorph.sourceforge.net/</a></span></p> </td> </tr> </tbody> </table> <p class="MsoNormal"><span>注意这里如果</span><span lang="EN-US">jakarta commons-lang </span><span>使用</span><span lang="EN-US">3.1</span><span>版本的会报:</span><span style="font-family:Consolas;color:red;font-size:10pt;" lang="EN-US">Caused by: </span><span style="text-decoration:underline;"><span style="font-family:Consolas;color:navy;font-size:10pt;" lang="EN-US">java.lang.ClassNotFoundException</span></span><span style="font-family:Consolas;color:red;font-size:10pt;" lang="EN-US">: </span><span style="text-decoration:underline;"><span style="font-family:Consolas;color:navy;font-size:10pt;" lang="EN-US">org.apache.commons.lang.exception.NestableRuntimeException</span></span></p> <p class="MsoNormal"><span>所以这里还是使用</span><span lang="EN-US">2.6</span><span>的吧</span></p> <p class="MsoNormal"> </p> <p class="MsoNormal"><span lang="EN-US">MyBean.java</span><span>:</span></p> <pre class="brush:java; toolbar: true; auto-links: false;">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; } } </pre> <span lang="EN-US">JsonlibTest.java</span> <span>:<pre class="brush:java; toolbar: true; auto-links: false;">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(); } } }</pre>转自:<a href="/misc/goto?guid=4959500829766002255" target="_blank">http://xdwangiflytek.iteye.com/blog/1345380</a></span> <p></p>
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!