说明
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()返回0age是 int 类型,应该调用ageJN.intValue(),
调用ageJN.doubleValue()返回20.0
如果调用ageJN.textValue()返回nullheight是 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<JsonNode> it = hobbyJN.iterator();
while(it.hasNext()){
JsonNode next = it.next();
System.out.println(next.textValue());
}
}
}