首頁 > 軟體

如何使用Jackson和JSON Pointer查詢解析任何JSON節點

2021-09-15 16:00:59

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:[email protected]
 */
@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();
    }
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com