<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 165, comments - 198, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    json 測試

    Posted on 2007-10-08 15:08 G_G 閱讀(1844) 評論(0)  編輯  收藏 所屬分類: json
    參照:
    ??? http://json-lib.sourceforge.net/usage.html

    先 json <-> object
    package?test.json;

    import?java.util.ArrayList;
    import?java.util.HashMap;
    import?java.util.List;
    import?java.util.Map;

    import?org.apache.commons.beanutils.PropertyUtils;

    import?com.jjm.viewBean.reportTableBean;

    import?net.sf.json.JSONArray;
    import?net.sf.json.JSONFunction;
    import?net.sf.json.JSONObject;

    import?junit.framework.TestCase;

    public?class?JsonTest?extends?TestCase?{

    //?object?to?json?************************************************************
    ????public?void?testList(){
    ????????
    boolean[]?boolArray?=?new?boolean[]{true,false,true};???
    ????????????JSONArray?jsonArray1?
    =?JSONArray.fromObject(?boolArray?);???
    ????????????System.out.println(?jsonArray1?);???
    ????????????
    //?prints?[true,false,true]??
    ????????????
    ????????????List?list?
    =?new?ArrayList();???
    ????????????list.add(?
    "first"?);???
    ????????????list.add(?
    "second"?);???
    ????????????JSONArray?jsonArray2?
    =?JSONArray.fromObject(?list?);???
    ????????????System.out.println(?jsonArray2?);???
    ????????????
    //?prints?["first","second"]??

    ????????????JSONArray?jsonArray3?
    =?JSONArray.fromObject(?"['json','is','easy']"?);???
    ????????????System.out.println(?jsonArray3?);???
    ????????????
    //?prints?["json","is","easy"]???
    ????}
    ????
    ????
    public?void?testMap(){
    ????????Map?map?
    =?new?HashMap();???
    ?????????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?);???
    ?????????
    //{"func":function(i){?return?this.arr[i];?},"arr":["a","b"],"int":1,"name":"json","bool":true}
    ????}

    ????
    /**
    ?????*?Bean.java?
    ????????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?void?testBean(){
    ?????????JSONObject?jsonObject?
    =?JSONObject.fromObject(?new?JsonBean()?);???
    ?????????System.out.println(?jsonObject?);???
    ?????????
    //{"func1":function(i){?return?this.options[i];?},"pojoId":1,"name":"json","options":["a","f"],"func2":function(i){?return?this.options[i];?}}??
    ?????}
    ?????
    ?????
    /**
    ??????*?private?int?row?;
    ??????????private?int?col?;
    ??????????private?String?value?;
    ??????*
    ??????
    */
    ?????
    public?void?testBeans(){
    ?????????List?list?
    =?new?ArrayList();
    ?????????JsonBean2?jb1?
    =?new?JsonBean2();
    ?????????jb1.setCol(
    1);
    ?????????jb1.setRow(
    1);
    ?????????jb1.setValue(
    "xx");
    ?????????
    ?????????JsonBean2?jb2?
    =?new?JsonBean2();
    ?????????jb2.setCol(
    2);
    ?????????jb2.setRow(
    2);
    ?????????jb2.setValue(
    "");
    ?????????
    ?????????
    ?????????list.add(jb1);
    ?????????list.add(jb2);
    ?????????
    ?????????JSONArray?ja?
    =?JSONArray.fromObject(list);
    ?????????System.out.println(?ja.toString()?);
    ?????????
    //[{"value":"xx","row":1,"col":1},{"value":"","row":2,"col":2}]
    ?????}
    ?????
    ?????

    //????json??to?object?************************************************************?????
    ?????
    ?????
    public?void?testJsonBeanUtil()throws?Exception{
    ?????????????String?json?
    =?"{name=\"json\",bool:true,int:1,double:2.2,func:function(a){?return?a;?},array:[1,2]}";???
    ????????????JSONObject?jsonObject?
    =?JSONObject.fromString(json);???
    ????????????Object?bean?
    =?JSONObject.toBean(?jsonObject?);???
    ????????????assertEquals(?jsonObject.get(?
    "name"?),?PropertyUtils.getProperty(?bean,?"name"?)?);???
    ????????????assertEquals(?jsonObject.get(?
    "bool"?),?PropertyUtils.getProperty(?bean,?"bool"?)?);???
    ????????????assertEquals(?jsonObject.get(?
    "int"?),?PropertyUtils.getProperty(?bean,?"int"?)?);???
    ????????????assertEquals(?jsonObject.get(?
    "double"?),?PropertyUtils.getProperty(?bean,?"double"?)?);???
    ????????????assertEquals(?jsonObject.get(?
    "func"?),?PropertyUtils.getProperty(?bean,?"func"?)?);???
    ????????????List?expected?
    =?JSONArray.toList(?jsonObject.getJSONArray(?"array"?)?);???
    ????????????assertEquals(?expected,?(List)?PropertyUtils.getProperty(?bean,?
    "array"?)?);??
    ?????}
    ?????
    ?????
    public?void?testJsonBean(){
    ?????????????String?json?
    =?"{\"value\":\"xx\",\"row\":1,\"col\":1}";???
    ????????????JSONObject?jsonObject?
    =?JSONObject.fromString(json);
    ????????????JsonBean2?bean?
    =?(JsonBean2)?JSONObject.toBean(?jsonObject,?JsonBean2.class?);???
    ????????????assertEquals(?jsonObject.get(?
    "col"?),new?Integer(?bean.getCol())??);???
    ????????????assertEquals(?jsonObject.get(?
    "row"?),?new?Integer(?bean.getRow()?)?);???
    ????????????assertEquals(?jsonObject.get(?
    "value"?),?bean.getValue()?);??
    ?????}
    }??? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???


    json <-> xml
    JSON to XML
    1. JSONObject?json?=?new?JSONObject(?true?);??
    2. String?xml?=?XMLSerializer.write(?json?);??
    1. <o?class="object"?null="true">??
    2. ??????
    1. JSONObject?json?=?JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");??
    2. String?xml?=?XMLSerializer.write(?json?);??
    1. <o?class="object">??
    2. ???<name?type="string">json</name>??
    3. ???<bool?type="boolean">true</bool>??
    4. ???<int?type="number">1</int>??
    5. </o>??
    1. JSONArray?json?=?JSONArray.fromObject("[1,2,3]");??
    2. String?xml?=?XMLSerializer.write(?json?);??
    1. <a?class="array"<??
    2. ???<e?type="number">1</e>??
    3. ???<e?type="number">2</e>??
    4. ???<e?type="number">3</e>??
    5. </a>??
    xml to json
    1. <a?class="array">??
    2. ??<e?type="function"?params="i,j">??
    3. ??????return?matrix[i][j];??
    4. ??</e>??
    5. </a>??
    1. JSONArray?json?=?(JSONArray)?XMLSerializer.read(?xml?);??
    2. System.out.println(?json?);??
    3. //?prints?[function(i,j){?return?matrix[i][j];?}]?



    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久免费99精品国产自在现线 | 67194成手机免费观看| 特级aa**毛片免费观看| 亚洲特级aaaaaa毛片| 亚洲精品无码国产| 亚洲乱理伦片在线观看中字| 久久久影院亚洲精品| 九九精品免费视频| 免费国产黄网站在线看| 亚洲欧美日韩中文高清www777| 亚洲av成人无码久久精品 | 日日摸夜夜添夜夜免费视频 | 桃子视频在线观看高清免费视频| 久久亚洲国产成人精品性色| 国产在线国偷精品产拍免费| 精品亚洲视频在线| 亚洲国产精品免费观看| 亚洲小视频在线播放| 国产一级高清视频免费看| 亚洲一区二区三区免费| 亚洲欧洲国产成人精品| 九月丁香婷婷亚洲综合色| 亚洲中文字幕无码日韩| 国产成人精品久久亚洲高清不卡 | 91在线手机精品免费观看| 青娱乐在线免费观看视频| 亚洲精品美女久久7777777| 亚洲欧美乱色情图片| 亚洲欧洲AV无码专区| 亚洲国产无线乱码在线观看| 亚洲中文无码亚洲人成影院| 亚洲日韩精品无码一区二区三区| 一区二区无码免费视频网站| aa在线免费观看| 成人无码精品1区2区3区免费看 | 在线看片v免费观看视频777| 国产免费一区二区三区| 免费在线观看的网站| 毛片a级毛片免费播放下载| 国产精品免费视频一区| 四虎影在线永久免费四虎地址8848aa|