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

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

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

    自由飛翔

    我在仰望,java之上

    統(tǒng)計(jì)

    留言簿(2)

    我關(guān)注的blog

    閱讀排行榜

    評(píng)論排行榜

    寫一個(gè)toJSON方法將Map對(duì)象轉(zhuǎn)換成JSON字符串

    /**
     * 
     * @author gavin
     *
     */
    public class Gson{
    public static String toJson(Map<String,String> map){
       Set<String> keys = map.keySet();
       String key = "";
       String value = "";
       StringBuffer jsonBuffer = new StringBuffer();
       jsonBuffer.append("{");    
       for(Iterator<String> it = keys.iterator();it.hasNext();){
           key =  (String)it.next();
           value = map.get(key);
           jsonBuffer.append(key+":"+value);
           if(it.hasNext()){
                jsonBuffer.append(",");
           }
       }
       jsonBuffer.append("}");
       return jsonBuffer.toString();
    }
    public static String toJson2(Map<String,String> map){
    Set<Map.Entry<String, String>> entrys = map.entrySet();
    Map.Entry<String, String> entry = null;
       String key = "";
       String value = "";
       StringBuffer jsonBuffer = new StringBuffer();
       jsonBuffer.append("{");    
       for(Iterator<Map.Entry<String, String>> it = entrys.iterator();it.hasNext();){
        entry =  (Map.Entry<String, String>)it.next();
        key = entry.getKey();
           value = entry.getValue();
           jsonBuffer.append(key+":"+value);
           if(it.hasNext()){
                jsonBuffer.append(",");
           }
       }
       jsonBuffer.append("}");
       return jsonBuffer.toString();
    }
    public static void main(String args[]){
    Map<String,String> map = new TreeMap<String,String>();
    map.put("1", "zhangyi");
    map.put("2", "zhanger");
    map.put("3", "zhangsan");
    map.put("4", "zhangsi");
    map.put("5", "zhangwu");
    System.out.println(toJson(map));
    System.out.println(toJson2(map));
    }
    }

    運(yùn)行結(jié)果:

    {1:zhangyi,2:zhanger,3:zhangsan,4:zhangsi,5:zhangwu}
    {1:zhangyi,2:zhanger,3:zhangsan,4:zhangsi,5:zhangwu}


    Gavin

    posted on 2011-08-19 00:23 GavinMiao 閱讀(11746) 評(píng)論(3)  編輯  收藏 所屬分類: corejava

    評(píng)論

    # re: 寫一個(gè)toJSON方法將Map對(duì)象轉(zhuǎn)換成JSON字符串 2012-03-22 15:58 釋皇天

    你的這個(gè)不行,你加一個(gè)這個(gè)試試。
    map.put("attributes", "{url=menu/toMaintain}");
    map.put("children", "[{id=102, text=路燈管理-根據(jù)局所ID查看配電箱, attributes={url=adminManage/findBoxByBureauId}, checked=true}, {id=103, text=路燈管理-根據(jù)局所ID查看道路, attributes={url=adminManage/findWayByBureauId}, checked=true}, {id=104, text=路燈管理-檢查是否有雙燈頭, attributes={url=adminManage/findLampBydisboxId}, checked=true}, {id=101, text=局所管理-異步查詢上級(jí)局所, attributes={url=bureau/getAllBurears}, checked=true}, {id=1, text=維護(hù)人員管理主頁(yè), attributes={url=man/tomaintainmanindex}, checked=true}, {id=2, text=維護(hù)人員-添加, attributes={url=man/addMan}, checked=true}, {id=3, text=維護(hù)人員-刪除, attributes={url=man/deleteMan}, checked=true}, {id=4, text=維護(hù)人員-修改, attributes={url=man/modifyMan}, checked=true}, {id=5, text=維護(hù)人員-查詢所有, attributes={url=man/getCurrentPageOfMan}, checked=true}, {id=6, text=維護(hù)人員與配電箱關(guān)聯(lián)管理主頁(yè), attributes={url=manbox/tomaintainboxindex}, checked=true}, {id=7, text=維護(hù)人員與配電箱關(guān)聯(lián)管理-添加, attributes={url=manbox/addMainBox}, checked=true}, {id=8, text=維護(hù)人員與配電箱關(guān)聯(lián)管理-刪除, attributes={url=manbox/deleteMainBox}, checked=true}, {id=9, text=維護(hù)人員與配電箱關(guān)聯(lián)管理-修改, attributes={url=manbox/modifyMainBox}, checked=true}, {id=10, text=維護(hù)人員與配電箱關(guān)聯(lián)管理-查詢所有, attributes={url=manbox/getCurrentPageOfMainbox}, checked=true}, {id=11, text=配電箱管理-查詢所有, attributes={url=box/getBoxes}, checked=true}, {id=22, text=獎(jiǎng)勵(lì)管理主頁(yè), attributes={url=encouragement/toencouragement}, checked=true}, {id=23, text=獎(jiǎng)勵(lì)管理-添加, attributes={url=encouragement/addEncouragement}, checked=true}, {id=24, text=獎(jiǎng)勵(lì)管理-刪除, attributes={url=encouragement/deleteEncouragement}, checked=true}, {id=25, text=獎(jiǎng)勵(lì)管理-修改, attributes={url=encouragement/modifyEncouragement}, checked=true}, {id=26, text=獎(jiǎng)勵(lì)管理-查詢所有, attributes={url=encouragement/getCurrentPageOfEncouragement}, checked=true}, {id=27, text=維護(hù)工單管理主頁(yè), attributes={url=dispatch/todispatch}, checked=true}, {id=28, text=維護(hù)工單管理-添加, attributes={url=dispatch/addDispatch}, checked=true}, {id=29, text=維護(hù)工單管理-刪除, attributes={url=dispatch/deleteDispatch}, checked=true}, {id=30, text=維護(hù)工單管理-修改, attributes={url=dispatch/modifyDispatch}, checked=true}, {id=31, text=維護(hù)工單管理-按id修改, attributes={url=dispatch/modifyByIdDispatch}, checked=true}, {id=32, text=維護(hù)工單管理-查詢所有, attributes={url=dispatch/getCurrentPageOfDispatch}, checked=true}, {id=33, text=維護(hù)工單管理-按id查詢, attributes={url=dispatch/getByIdDispatch}, checked=true}, {id=34, text=工程驗(yàn)收管理主頁(yè), attributes={url=projectm/toprojectm}, checked=true}, {id=35, text=工程驗(yàn)收管理-添加, attributes={url=projectm/addProjectm}, checked=true}, {id=36, text=工程驗(yàn)收管理-刪除, attributes={url=projectm/deleteProjectm}, checked=true}, {id=37, text=工程驗(yàn)收管理-修改, attributes={url=projectm/modifyPMAction}, checked=true}, {id=38, text=工程驗(yàn)收管理-查詢所有, attributes={url=projectm/getCurrentPageOfProjectm}, checked=true}, {id=58, text=局所管理主頁(yè), attributes={url=bureau/tobureauindex}, checked=true}, {id=59, text=局所管理-添加, attributes={url=bureau/saveBureau}, checked=true}, {id=60, text=局所管理-刪除, attributes={url=bureau/deleteBureau}, checked=true}, {id=61, text=局所管理-修改, attributes={url=bureau/updateBureau}, checked=true}, {id=62, text=局所管理-查詢所有, attributes={url=bureau/getAllBur}, checked=true}, {id=63, text=局所管理-查詢id、nane, attributes={url=bureau/getBureauIdAndName}, checked=true}, {id=64, text=局所管理-查詢上級(jí)局所, attributes={url=bureau/getUpperBureaus}, checked=true}, {id=65, text=配電箱管理主頁(yè), attributes={url=disbox/todisboxindex}, checked=true}, {id=66, text=配電箱管理-添加, attributes={url=disbox/saveDistributionBox}, checked=true}, {id=67, text=配電箱管理-刪除, attributes={url=disbox/deleteDistributionBox}, checked=true}, {id=68, text=配電箱管理-修改, attributes={url=disbox/updateDistributionBox}, checked=true}, {id=69, text=配電箱管理-查詢所有, attributes={url=disbox/getAllDisbox}, checked=true}, {id=70, text=路燈管理主頁(yè), attributes={url=lamp/tolampindex}, checked=true}, {id=71, text=路燈管理-添加, attributes={url=lamp/saveLamp}, checked=true}, {id=72, text=路燈管理-刪除, attributes={url=lamp/deleteLamp}, checked=true}, {id=73, text=路燈管理-修改, attributes={url=lamp/updateLamp}, checked=true}, {id=74, text=路燈管理-查詢所有, attributes={url=lamp/getAllLamps}, checked=true}]");

      回復(fù)  更多評(píng)論   

    # re: 寫一個(gè)toJSON方法將Map對(duì)象轉(zhuǎn)換成JSON字符串 2012-03-22 15:59 釋皇天

    就是,如果mao.put("str","這里如果是個(gè)數(shù)組,或者map就不行了。")  回復(fù)  更多評(píng)論   

    # re: 寫一個(gè)toJSON方法將Map對(duì)象轉(zhuǎn)換成JSON字符串 2013-04-15 14:13 xiocun

    http://dufeifei.iteye.com/blog/1571005  回復(fù)  更多評(píng)論   

    主站蜘蛛池模板: 亚洲日日做天天做日日谢| 边摸边吃奶边做爽免费视频99| 国内精品乱码卡1卡2卡3免费| 亚洲人成电影网站免费| 亚洲精品无码99在线观看| 最近中文字幕大全免费版在线| 亚洲国产午夜精品理论片| 波多野结衣中文一区二区免费| 免费看一区二区三区四区| 国产精品亚洲精品观看不卡| 久久久久国产成人精品亚洲午夜 | 国产成人青青热久免费精品 | 岛国精品一区免费视频在线观看 | 高清免费久久午夜精品 | 麻豆狠色伊人亚洲综合网站| 内射无码专区久久亚洲| 一级毛片免费观看不卡的| 美女扒开尿口给男人爽免费视频| 亚洲欧洲日产国码久在线观看| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲a∨无码精品色午夜| 亚洲尤码不卡AV麻豆| 成人影片麻豆国产影片免费观看| 国产美女视频免费观看的网站 | 91天堂素人精品系列全集亚洲 | 亚洲乱码卡一卡二卡三| 亚洲尤码不卡AV麻豆| 国产成人精品123区免费视频| 95免费观看体验区视频| 一级做a毛片免费视频| 亚洲人av高清无码| 亚洲国产综合精品| 亚洲国产精品成人精品无码区在线| 在线观看永久免费视频网站| 亚洲人成在线免费观看| 久久精品免费观看| 一个人看的免费观看日本视频www 一个人看的免费视频www在线高清动漫 | 无码精品人妻一区二区三区免费看 | 亚洲五月六月丁香激情| 亚洲精品线路一在线观看| 国产精品免费观看久久|