字串值提取工具-09-java 執行 json 解析, json-path

老马啸西风發表於2024-08-18

值提取系列

字串值提取工具-01-概覽

字串值提取工具-02-java 呼叫 js

字串值提取工具-03-java 呼叫 groovy

字串值提取工具-04-java 呼叫 java? Janino 編譯工具

字串值提取工具-05-java 呼叫 shell

字串值提取工具-06-java 呼叫 python

字串值提取工具-07-java 呼叫 go

字串值提取工具-08-java 透過 xml-path 解析 xml

字串值提取工具-09-java 執行 json 解析, json-path

字串值提取工具-10-java 執行表示式引擎

擴充閱讀

如果你對 json-path 不是很熟悉,建議學習:

Json Path-另一種解析 json 的方式 jsonpath

場景

我們希望透過 java 執行 json-path 解析 json。

核心實現

package com.github.houbb.value.extraction.core.support.extraction;

import com.github.houbb.value.extraction.api.ValueExtractionContext;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ReadContext;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 *
 * @since 0.6.0
 */
public class ValueExtractionJsonPath extends AbstractValueExtractionAdaptor<ReadContext> {

    @Override
    protected ReadContext prepare(ValueExtractionContext context) {
        final String json = (String) context.getDataMap().get("json");

        // 避免多次解析
        Configuration configuration = Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build();
        return JsonPath.parse(json, configuration);
    }

    @Override
    protected Object evaluate(ReadContext prepareObject, String script, ValueExtractionContext context) {
        return prepareObject.read(script);
    }

}

測試例子

public void test() {
        String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";

        // 測試 getValueByXPath 方法
        String script = "$.store.book[1].author";

        // 建立繫結並設定引數
        Map<String, Object> bindings = new HashMap<>();
        bindings.put("json", json);

        String result = ValueExtractionBs.newInstance()
                .scripts(Arrays.asList(script))
                .valueExtraction(ValueExtractions.jsonPath())
                .dataMap(bindings)
                .extract().toString();

        Assert.assertEquals("{$.store.book[1].author=Evelyn Waugh}", result);
    }

相關文章