重大改版
 鱼翔空 2013-06-20 java语言,java基础|   0 7449 
文章评分 1 次,平均分 5

今天在做一个校验的功能,为了通用,在页面上使用json字符串配置要校验的所有项和表.在后台转map的时候一层一层的转太麻烦了,就写了个通用 方法来转.

转换方法:

/**
     * json字符串转map集合
     * @author ducc
     * @param jsonStr json字符串
     * @param map 接收的map
     * @return
     */
    public Map<String, Object> json2Map(String jsonStr,Map<String, Object> map){
        JSONObject jsonObject = JSONObject.fromObject(jsonStr);    
        map = JSONObject.fromObject(jsonObject);
        //递归map的value,如果
        for(Entry<String, Object> entry:map.entrySet()){
            json2map1(entry, map);
        }
        return map;
    }
    /**
     * json转map,递归调用的方法
     * @author ducc
     * @param entry 
     * @param map
     * @return
     */
    public Map<String, Object> json2map1(Entry<String, Object> entry,Map<String, Object> map){
        if(entry.getValue() instanceof Map){
             JSONObject jsonObject= JSONObject.fromObject(entry.getValue());    
             Map<String, Object> map1 = JSONObject.fromObject(jsonObject);
               
             for(Entry<String, Object> entry1:map1.entrySet()){
                 map1=json2map1(entry1, map1);
                 map.put(entry.getKey(), map1);
             }
        }
         return map;
    }

测试方法:

@Test
    public void aa(){
        String jsonStr="{'qyyzcry':{'TABLES':'PROCESS_RYJBXX r,PRCOESS_RYKZXXB k,PROCESS_QYRYGXB g','TABLESLINK':'r.ID=k.RYID and g.RYID=r.ID and g.rylx=\"QYRYFXB009\"','PROCESS_RYJBXX':'r.ZWXM,r.ZGXL,r.ZJLX,r.ZJHM','PRCOESS_RYKZXXB':'k.ZW,k.ZC,k.CSZY','testa':{'testb':{'testc':'testctest'},'testb1':'testb1text'}}}";
        Map<String, Object> map =new HashMap<String, Object>(); //JSONObject.fromObject(jsonObject);
        map=json2Map(jsonStr, map);
        for(Entry<String, Object> entry:map.entrySet()){
            System.err.println("key: "+entry.getKey()+"---value:"+entry.getValue());
            Map<String, Object> m=(Map<String, Object>) entry.getValue();
            for(Entry<String, Object> e:m.entrySet()){
                System.err.println("key: "+e.getKey()+"---value:"+e.getValue());
                if(e.getValue() instanceof Map){
                    Map<String, Object> objmap=(Map<String, Object>) e.getValue();
                    for(Entry<String, Object> en:objmap.entrySet()){
                        System.err.println("key: "+en.getKey()+"---value:"+en.getValue());
                    }
                }
            }
        }
    }

输出结果:

key: qyyzcry---value:{"TABLES":"PROCESS_RYJBXX r,PRCOESS_RYKZXXB k,PROCESS_QYRYGXB g","TABLESLINK":"r.ID=k.RYID and g.RYID=r.RYID and g.rylx=\"QYRYFXB009\"","PROCESS_RYJBXX":"r.ZWXM,r.ZGXL,r.ZJLX,r.ZJHM","PRCOESS_RYKZXXB":"k.ZW,k.ZC,k.CSZY","testa":{"testb":{"testc":"testctest"},"testb1":"testb1text"}}

key: TABLES---value:PROCESS_RYJBXX r,PRCOESS_RYKZXXB k,PROCESS_QYRYGXB g

key: TABLESLINK---value:r.ID=k.RYID and g.RYID=r.RYID and g.rylx="QYRYFXB009"

key: PROCESS_RYJBXX---value:r.ZWXM,r.ZGXL,r.ZJLX,r.ZJHM

key: PRCOESS_RYKZXXB---value:k.ZW,k.ZC,k.CSZY

key: testa---value:{"testb":{"testc":"testctest"},"testb1":"testb1text"}

key: testb---value:{"testc":"testctest"}

key: testb1---value:testb1text

 

 

除特别注明外,本站所有文章均为我要编程原创,转载请注明出处来自http://5ycode.com/article/250.html

关于
该用户很懒!
切换注册

登录

忘记密码 ?

切换登录

注册

扫一扫二维码分享