生活随笔
收集整理的這篇文章主要介紹了
JSON-lib 使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://weishuwei.iteye.com/blog/97384
json-lib.jar開發包使用:
依賴包: commons-beanutils.jar;
commons-httpclient.jar;
commons-lang.jar;
ezmorph.jar;不少人使用時會提示net.sf.ezmorph.xxx找不到,就是缺這個:
morph-1.0.1.jar
相關鏈接: http://json-lib.sourceforge.net/ http://ezmorph.sourceforge.net/ http://morph.sourceforge.net/ 使用過程中問題: 1,把bean轉化為json格式時老提示如下錯誤:
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: Property 'name' has no getter method
解決:聲明bean為public class xxx,必須是public,我用默認類型(class xxx)都不行
2,Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.lang.ArrayUtils.toObject([C)[Ljava/lang/Character;
原因:定義屬性如下:private char[] options = new char[] { 'a', 'f' };好像不能處理這種類型的
3, private String func1 = "function(i){ return this.options[i]; }";
?? 和
?? private JSONFunction func2 = new JSONFunction(new String[] { "i" },
???? "return this.options[i];");
?? 轉換后顯示結果差不多:
?? {"func1":function(i){ return this.options[i];,"func2":function(i){ return this.options[i]; }}
測試類:
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;???? public ?class ?Json?{??????public ?static ?void ?main(String[]?args)?{?? ????????Json?j?=?new ?Json();?? ????????j.bean2json();?? ????}?? ?? ????public ?void ?arr2json()?{?? ????????boolean []?boolArray?=?new ?boolean []?{?true ,?false ,?true ?};?? ????????JSONArray?jsonArray?=?JSONArray.fromObject(boolArray);?? ????????System.out.println(jsonArray);?? ?????????? ????}?? ?? ????public ?void ?list2json()?{?? ????????List?list?=?new ?ArrayList();?? ????????list.add("first" );?? ????????list.add("second" );?? ????????JSONArray?jsonArray?=?JSONArray.fromObject(list);?? ????????System.out.println(jsonArray);?? ?????????? ????}?? ?? ????public ?void ?createJson()?{?? ????????JSONArray?jsonArray?=?JSONArray.fromObject("['json','is','easy']" );?? ????????System.out.println(jsonArray);?? ?????????? ????}?? ?? ????public ?void ?map2json()?{?? ????????Map ????????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?json?=?JSONObject.fromObject(map);?? ????????System.out.println(json);?? ?????????? ?????????? ?????????? ????}?? ?? ????public ?void ?bean2json()?{?? ????????JSONObject?jsonObject?=?JSONObject.fromObject(new ?MyBean());?? ????????System.out.println(jsonObject);?? ????????? ? ? ? ? ?? ????}?? ?? ????public ?void ?json2bean()?{?? ????????String?json?=?"{name=\"json2\",func1:true,pojoId:1,func2:function(a){?return?a;?},options:['1','2']}" ;?? ????????JSONObject?jb?=?JSONObject.fromString(json);?? ????????JSONObject.toBean(jb,?MyBean.class );?? ????????System.out.println();?? ????}?? }??
操作的bean:
import ?net.sf.json.JSONFunction;???? public ?class ?MyBean?{??????private ?String?name?=?"json" ;?? ????private ?int ?pojoId?=?1 ;?? ?????? ????private ?String?func1?=?"function(i){?return?this.options[i];?}" ;?? ????private ?JSONFunction?func2?=?new ?JSONFunction(new ?String[]?{?"i" ?},?? ????????????"return?this.options[i];" );?? ?? ?????? ????......?? }??
題外話: 這個我對json-lib包的初次嘗試,希望對大家有所幫助,另外大家有誰用過其它處理json的開發包,提出來,大家探討一下~!!!!
=============
java中使用net.sf.json對json進行解析 2010-08-02 15:44 1869人閱讀 收藏 舉報 net.sf.json依賴的包很多。
有commons-collections,commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph-1.0.5.jar,morph-1.1.1.jar
? ? ? ? ? ? ?? ????public ?static ?Object?getDTO(String?jsonString,?Class?clazz){ ?? ????????JSONObject?jsonObject?=?null ; ?? ????????try { ?? ????????????setDataFormat2JAVA();? ?? ????????????jsonObject?=?JSONObject.fromObject(jsonString); ?? ????????}catch (Exception?e){ ?? ????????????e.printStackTrace(); ?? ????????} ?? ????????return ?JSONObject.toBean(jsonObject,?clazz); ?? ????} ?? ???? ?? ????? ? ? ? ? ? ? ? ?? ????public ?static ?Object?getDTO(String?jsonString,?Class?clazz,?Map?map){ ?? ????????JSONObject?jsonObject?=?null ; ?? ????????try { ?? ????????????setDataFormat2JAVA();? ?? ????????????jsonObject?=?JSONObject.fromObject(jsonString); ?? ????????}catch (Exception?e){ ?? ????????????e.printStackTrace(); ?? ????????} ?? ????????return ?JSONObject.toBean(jsonObject,?clazz,?map); ?? ????} ?? ???? ?? ????? ? ? ? ? ? ?? ????public ?static ?Object[]?getDTOArray(String?jsonString,?Class?clazz){ ?? ????????setDataFormat2JAVA(); ?? ????????JSONArray?array?=?JSONArray.fromObject(jsonString); ?? ????????Object[]?obj?=?new ?Object[array.size()]; ?? ????????for (int ?i?=?0 ;?i?<?array.size();?i++){ ?? ????????????JSONObject?jsonObject?=?array.getJSONObject(i); ?? ????????????obj[i]?=?JSONObject.toBean(jsonObject,?clazz); ?? ????????} ?? ????????return ?obj; ?? ????} ?? ???? ?? ????? ? ? ? ? ? ? ?? ????public ?static ?Object[]?getDTOArray(String?jsonString,?Class?clazz,?Map?map){ ?? ????????setDataFormat2JAVA(); ?? ????????JSONArray?array?=?JSONArray.fromObject(jsonString); ?? ????????Object[]?obj?=?new ?Object[array.size()]; ?? ????????for (int ?i?=?0 ;?i?<?array.size();?i++){ ?? ????????????JSONObject?jsonObject?=?array.getJSONObject(i); ?? ????????????obj[i]?=?JSONObject.toBean(jsonObject,?clazz,?map); ?? ????????} ?? ????????return ?obj; ?? ????} ?? ???? ?? ????? ? ? ? ? ?? ????public ?static ?List?getDTOList(String?jsonString,?Class?clazz){ ?? ????????setDataFormat2JAVA(); ?? ????????JSONArray?array?=?JSONArray.fromObject(jsonString); ?? ????????List?list?=?new ?ArrayList(); ?? ????????for (Iterator?iter?=?array.iterator();?iter.hasNext();){ ?? ????????????JSONObject?jsonObject?=?(JSONObject)iter.next(); ?? ????????????list.add(JSONObject.toBean(jsonObject,?clazz)); ?? ????????} ?? ????????return ?list; ?? ????} ?? ???? ?? ????? ? ? ? ? ? ?? ????public ?static ?List?getDTOList(String?jsonString,?Class?clazz,?Map?map){ ?? ????????setDataFormat2JAVA(); ?? ????????JSONArray?array?=?JSONArray.fromObject(jsonString); ?? ????????List?list?=?new ?ArrayList(); ?? ????????for (Iterator?iter?=?array.iterator();?iter.hasNext();){ ?? ????????????JSONObject?jsonObject?=?(JSONObject)iter.next(); ?? ????????????list.add(JSONObject.toBean(jsonObject,?clazz,?map)); ?? ????????} ?? ????????return ?list; ?? ????} ?? ???? ?? ????? ? ? ? ? ? ?? ????public ?static ?Map?getMapFromJson(String?jsonString)?{ ?? ????????setDataFormat2JAVA(); ?? ????????JSONObject?jsonObject?=?JSONObject.fromObject(jsonString); ?? ????????Map?map?=?new ?HashMap(); ?? ????????for (Iterator?iter?=?jsonObject.keys();?iter.hasNext();){ ?? ????????????String?key?=?(String)iter.next(); ?? ????????????map.put(key,?jsonObject.get(key)); ?? ????????} ?? ????????return ?map; ?? ????} ?? ???? ?? ????? ? ? ? ? ?? ????public ?static ?Object[]?getObjectArrayFromJson(String?jsonString)?{ ?? ????????JSONArray?jsonArray?=?JSONArray.fromObject(jsonString); ?? ????????return ?jsonArray.toArray(); ?? ????} ?? ?? ?? ????? ? ? ? ? ? ? ?? ????public ?static ?String?getJSONString(Object?object)?throws ?Exception{ ?? ????????String?jsonString?=?null ; ?? ?????????? ????????JsonConfig?jsonConfig?=?new ?JsonConfig(); ?? ????????jsonConfig.registerJsonValueProcessor(java .util.Date.class ,?new ?JsonDateValueProcessor()); ?? ????????if (object?!=?null ){ ?? ????????????if (object?instanceof ?Collection?||?object?instanceof ?Object[]){ ?? ????????????????jsonString?=?JSONArray.fromObject(object,?jsonConfig).toString(); ?? ????????????}else { ?? ????????????????jsonString?=?JSONObject.fromObject(object,?jsonConfig).toString(); ?? ????????????} ?? ????????} ?? ????????return ?jsonString?==?null ???"{}" ?:?jsonString; ?? ????}???
總結
以上是生活随笔 為你收集整理的JSON-lib 使用 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。