java json字符串转map集合递归嵌套

2013/06/20 7397点热度 2人点赞 0条评论

今天在做一个校验的功能,为了通用,在页面上使用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

 

yxkong

这个人很懒,什么都没留下

文章评论