json字符串转成java的map与list复合对象的解决方法

开发时需要将json字符串转成java的map和list,找了半天竟然没有一个现成的的包支持这种功能!不可思议,苦苦在stackoverflow上求医问药,终于找到jackson的一个特性,可以生成json的节点树,这才终于解决了这个头疼的问题。

代码引用了jackson2.7.3的两个包:

       
            com.fasterxml.jackson.core
            jackson-core
            2.7.3
       

       
            com.fasterxml.jackson.core
            jackson-databind
            2.7.3
       

递归遍历每个节点然后判断节点的类型做转换,代码如下:

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.*;


/**
 * Created by fineTu on 2016/09/27.
 */
public class JsonUtil {



    public static Map parse(String jsonStr) throws IOException {
        Map resMap = new HashMap();
        JsonFactory factory = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper(factory);
        JsonNode rootNode = mapper.readTree(jsonStr);


        resMap = parseNode2Obj(rootNode);
        return resMap;
    }


    private static Map parseNode2Obj(JsonNode jsonNode){
        Map resMap = new HashMap();
        if(jsonNode.isValueNode()){
            Object value = parseValue(jsonNode);
            resMap.put("",value);
            return resMap;
        }else if(jsonNode.isArray()){
            List list = new ArrayList();
            Iterator it = jsonNode.iterator();
            while( it.hasNext()){
                Map child = parseNode2Obj(it.next());
                if (child.keySet().size() == 1 && child.keySet().contains("")){
                    list.add(child.get(""));
                }else{
                    list.add(child);
                }
            }
            resMap.put("",list);
            return resMap;
        }else {
            Iterator> it = jsonNode.fields();
            while( it.hasNext()){
                Map.Entry entry = it.next();
                Map child = parseNode2Obj(entry.getValue());
                if (child.keySet().size() == 1 && child.keySet().contains("")){
                    resMap.put(entry.getKey(),child.get(""));
                }else{
                    resMap.put(entry.getKey(),child);
                }
            }
            return resMap;
        }
    }


    private static Object parseValue(JsonNode valueNode){
        if(valueNode.isTextual()){
            return valueNode.asText();
        }else if(valueNode.isInt()){
            return valueNode.asInt();
        }else if(valueNode.isBigInteger() || valueNode.isLong()){
            return valueNode.asLong();
        }else if(valueNode.isFloat() || valueNode.isDouble()){
            return valueNode.asDouble();
        }else if(valueNode.isBoolean()){
            return valueNode.asBoolean();
        }
        return valueNode.asText();
    }
    public static void main(String[] args){
        String jsonStr = "{\"a\":12,\"b\":\"foobar\",\"c\":[\"tom\",\"peter\",\"joe\"],\"d\":{\"da\":\"1123\",\"db\":456,\"dc\":[{\"name\":\"super man\",\"id\":1},{\"name\":\"bat man\",\"id\":2}]}}";
        try {
            Map resMap = JsonUtil.parse(jsonStr);
            System.out.println(resMap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


} 
  

代码贡献出来,节省你的时间

转载请注明出处:http://blog.csdn.net/gklifg/article/details/52680854

 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(java,java,json,map,list)