概述
Jackson和JSON Pointer查询解析任何JSON节点
JSON Pointer是字符串表达式,用于标识JSON文档特定节点。RFC 6901规范有定义,用于查询复杂Json文档结构。
1.示例Json文档
{ "firstName": "John", "lastName": "Doe", "address": { "street": "21 2nd Street", "city": "New York", "postalCode": "10021-3100", "coordinates": { "latitude": 40.7250387, "longitude": -73.9932568 } }, "phone": [ "139", "137" ], "grade": [ { "name": "math", "score": 99 }, { "name": "english", "score": 97 } ] }
该文档包括复杂的嵌套,其中嵌套类型有对象,数组,数组对象。下面使用JSON Pointer进行解析。
2. 解析文档
使用Jackson的核心对象ObjectMapper,首先解析json字符串未JsonNode。
String json = "{n" + " "firstName": "John",n" + " "lastName": "Doe",n" + " "address": {n" + " "street": "21 2nd Street",n" + " "city": "New York",n" + " "postalCode": "10021-3100",n" + " "coordinates": {n" + " "latitude": 40.7250387,n" + " "longitude": -73.9932568n" + " }n" + " },n" + " "phone":["139","137"],n" + " "grade":[n" + " {"name":"math","score":99},n" + " {"name":"english","score":97}n" + " ]n" + " }"; ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(json);
2.1 获取属性
JsonNode firstName = node.at("/firstName"); print("firstName",firstName.toString());
必须以/开头,表示当前起始节点。
输出结果:
firstName:"John"
2.2 获取对象属性
JsonNode coordinatesNode = node.at("/address/coordinates"); print("coordinatesNode",coordinatesNode.toString());
输出结果:
coordinatesNode:{"latitude":40.7250387,"longitude":-73.9932568}
2.3 获取数组属性
JsonNode phoneNode = node.at("/phone"); print("phoneNode", phoneNode.toString());
输出结果:
phoneNode:["139","137"]
2.4 获取数组属性元素
JsonNode phone1Node = node.at("/phone/0"); print("phone1Node",phone1Node.toString());
输出结果:
phone1Node:"139"
2.4 获取数组对象属性
JsonNode nameNode = node.at("/grade/0/name"); print("name",nameNode.toString()); JsonNode scoreNode = node.at("/grade/0/score"); print("score",scoreNode.toString());
输出结果:
name:"math"
score:99
Jackson 通用解析JSON方法
package com.util; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.type.TypeFactory; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * json 解析类,通用于全项目 * <p/> * Created by 刘一波 on 15/7/24. * E-Mail:zhanlanstar@163.com */ @Slf4j public class JsonUtils { private static ObjectMapper objectMapper = new ObjectMapper(); static { objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); objectMapper.setDateFormat(sdf); } /** * 提供给elasticsearch使用,把bean转换成list map 集合类型,否则不能存入索引 * * @param o * @return */ public static Object beanToJsonObject(Object o) { return jsonStrToList(objectToJsonStr(o), Map.class); } public static String objectToJsonStr(Object o) { try { return objectMapper.writeValueAsString(o); } catch (IOException e) { log.error("object can not objectTranslate to json", e); } return null; } public static <T> T jsonStrToObject(String json, Class<T> cls) { try { return objectMapper.readValue(json, cls); } catch (IOException e) { log.error("json cant be objectTranslate to object", e); return null; } } public static <T> T jsonDataToObject(String jsonStr, Class<T> cls) { if (!StringUtils.isEmpty(jsonStr)) { T data = JsonUtils.jsonStrToObject(jsonStr, cls); return data; } else { return null; } } public static <T> List<T> jsonStrToList(String jsonStr, Class<?> clazz) { List<T> list = Lists.newArrayList(); try { // 指定容器结构和类型(这里是ArrayList和clazz) TypeFactory t = TypeFactory.defaultInstance(); list = objectMapper.readValue(jsonStr, t.constructCollectionType(ArrayList.class, clazz)); } catch (IOException e) { log.error("反序列化序列化attributes,从Json到List报错", e); } return list; } public static Map jsonStrToMap(String attributes) { try { return objectMapper.readValue(attributes, HashMap.class); } catch (IOException e) { log.error("反序列化序列化attributes,从Json到HashMap报错", e); } return new HashMap(); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。
最后
以上就是霸气睫毛为你收集整理的如何使用Jackson和JSON Pointer查询解析任何JSON节点的全部内容,希望文章能够帮你解决如何使用Jackson和JSON Pointer查询解析任何JSON节点所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复