Jackson:json字符串转JsonNode对象 作者:马育民 • 2022-09-22 20:13 • 阅读:10609 # 说明 Jackson 可以将 json字符串 转成 JsonNode 对象,该对象是 Jackson 内置的对象,用于封装json字符串的,并提供很多方法 ### 优点 优点较多,主要之一:当字符串 **不是 json 格式** 时,程序也不会报错 下面程序可以正常运行: ``` String s = "111"; // 不是json格式 ObjectMapper om = new ObjectMapper(); JsonNode jsonNode = om.readTree(s); System.out.println(); ``` # 例子 ``` package jackson; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; public class Test1 { public static void main(String[] args) throws IOException { String s = "{\"name\":\"李雷\",\"age\":20,\"height\":1.82}"; ObjectMapper om = new ObjectMapper(); JsonNode jsonNode = om.readTree(s); JsonNode nameJN = jsonNode.get("name"); JsonNode ageJN = jsonNode.get("age"); JsonNode heightJN = jsonNode.get("height"); JsonNode sexJN = jsonNode.get("sex"); // 获取不存在的key,返回null System.out.println("nameJN.textValue():"+nameJN.textValue()); System.out.println("nameJN.textValue():"+nameJN.intValue()); System.out.println("ageJN.intValue():"+ageJN.intValue()); System.out.println("ageJN.doubleValue():"+ageJN.doubleValue()); System.out.println("ageJN.textValue():"+ageJN.textValue()); System.out.println("heightJN.doubleValue():"+heightJN.doubleValue()); System.out.println("heightJN.intValue():"+heightJN.intValue()); System.out.println("heightJN.textValue():"+heightJN.textValue()); System.out.println(sexJN); } } ``` 执行结果: ``` nameJN.textValue():李雷 nameJN.textValue():0 ageJN.intValue():20 ageJN.doubleValue():20.0 ageJN.textValue():null heightJN.doubleValue():1.82 heightJN.intValue():1 heightJN.textValue():null null // 获取不存在的key,返回null ``` ### JsonNode对象 的 xxxValue() 方法 该方法与 **值类型 相关**: - `name` 是字符串,应该调用 `nameJN.textValue()` 。 如果调用 `nameJN.intValue()` 返回 `0` - `age` 是 **int** 类型,应该调用 `ageJN.intValue()` , 调用 `ageJN.doubleValue()` 返回 `20.0` 如果调用 `ageJN.textValue()` 返回 `null` - `height` 是 **double** 类型,应该调用 `heightJN.doubleValue()` 调用 `heightJN.intValue()` 返回 `1` 如果调用 `heightJN.textValue()` 返回 `null` ### JsonNode对象 的 asText() 方法 可以将基本数据类型转成 **字符串** ``` System.out.println("ageJN:"+ageJN.asText()); System.out.println("heightJN:"+heightJN.asText()); System.out.println("hobbyJN:"+hobbyJN.asText()); ``` 执行结果: ``` ageJN:20 heightJN:1.82 hobbyJN: // hobbyJN 是字符串数组,调用 asText() 返回空字符串 ``` ### JsonNode对象 的 asXxx() 方法 该方法可以做 **类型转换** ``` String s = "{\"age\":\"20\"}"; ObjectMapper om = new ObjectMapper(); JsonNode jsonNode = om.readTree(s); JsonNode ageJN = jsonNode.get("age"); // age的值 "20",有双引号,是 字符串类型,所以 调用 intValue() 返回 0 System.out.println("ageJN:"+ageJN.intValue()); // 调用 asInt() 才能返回正确的值 System.out.println("ageJN:"+ageJN.asInt()); ``` 执行结果: ``` ageJN:0 ageJN:20 ``` ### JsonNode 的 asXxx( 默认值 ) 方法 当类型转换错误时,返回默认值 ``` System.out.println("nameJN:"+nameJN.asInt(1)); ``` 执行结果: ``` nameJN:1 ``` 因为 `name` 的值是 `李雷` ,转换成 `int` 类型会失败,所以返回默认值 `1` # 遍历数组 ``` import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.util.Iterator; public class Test5遍历数组 { public static void main(String[] args) throws IOException { String s = "{\"hobby\":[\"抖音\",\"王者荣耀\",\"原神\"]}"; ObjectMapper om = new ObjectMapper(); JsonNode jsonNode = om.readTree(s); JsonNode hobbyJN = jsonNode.get("hobby"); Iterator it = hobbyJN.iterator(); while(it.hasNext()){ JsonNode next = it.next(); System.out.println(next.textValue()); } } } ``` 原文出处:http://malaoshi.top/show_1IX4624r3bHj.html