Json 和 JsonNode 区别 和 转换

Json

 String json = "{\"username\":\"zhangsan\",\"性别\":\"男\",\"company\":{\"companyName\":\"微软\",\"address\":\"美国\"},\"cars\":[\"电车\",\"单车\"]}";  

打印:

{"username":"zhangsan","性别":"男","company":{"companyName":"微软","address":"美国"},"cars":["电车","单车"]}

JsonNode

ObjectMapper mapper = new ObjectMapper();  
            //JSON ----> JsonNode  
            JsonNode rootNode = mapper.readTree(json);    
            Iterator keys = rootNode.fieldNames();     
            while(keys.hasNext()){    
                String fieldName = keys.next();    
                System.out.println(fieldName + ": " + rootNode.path(fieldName).toString());    
            }

打印:

username: "zhangsan"
性别: "男"
company: {"companyName":"微软","address":"美国"}
cars: ["电车","单车"]

你可能感兴趣的:(Json 和 JsonNode 区别 和 转换)