com.github.houbb.value.extraction.test.bs.JsonTest Maven / Gradle / Ivy
package com.github.houbb.value.extraction.test.bs;
import com.github.houbb.value.extraction.core.bs.ValueExtractionBs;
import com.github.houbb.value.extraction.core.support.extraction.ValueExtractions;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class JsonTest {
@Test
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 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);
}
}