在 Jayway JsonPath 中,concat() 函数用于连接两个或多个字符串。concat() 函数可以接受两个或多个参数,并将它们连接成一个字符串。这些参数可以是字面量字符串,也可以是 JSON 文档中的表达式。
注意:标准的 JSONPath 规范中并没有定义 concat() 函数,这是 Jayway JSONPath 实现特有的一个功能。
Java 示例:
package com.hxstrive.json_path.function; import com.jayway.jsonpath.JsonPath; /** * concat() 函数 * @author hxstrive.com */ public class FunctionDemo08 { public static void main(String[] args) { String json = "{" + " \"store\": {" + " \"book\": [" + " {" + " \"category\": \"reference\"," + " \"author\": \"Nigel Rees\"," + " \"title\": \"Sayings of the Century\"," + " \"price\": 8.95" + " }," + " {" + " \"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" + "}"; Object obj = JsonPath.read(json, "concat(\"color=\",$..bicycle.color, \", price=\", $..bicycle.price)"); System.out.println(obj); } }
运行示例,输出如下:
color=red price=19.95