JSONPath 和 Traverson 过滤:单元素数组
JSONPath and Traverson filtering: single element array
这是我试图与 Traverson 一起处理的 Json 以从此 HAL 中提取单个 link:
{
"_links": {
"curies": [
{
"href": "https://localhost/auth/def/rels/{rel}",
"name": "auth",
"templated": true
}
],
"self": {
"href": "https://localhost/auth/identity"
}
},
"_embedded": {
"auth": [
{
"_links": {
"auth:ad": [
{
"href": "https://localhost/auth/ad"
}
]
},
"kind": "oauth2"
},
{
"_links": {
"auth:default": [
{
"href": "https://localhost/auth/default" // **Link that I need**
}
]
},
"kind": "oauth2"
}
]
}
}
Java 理想情况下负责处理的代码应该如下所示:
Link test = traverson
.follow("auth")
.follow("$.['_embedded']['auth']..['_links']['auth:default']..['href'][0]")
.asLink();
但是由于某种原因,尽管在 JSONPath 表达式中添加最后一个 [0]
之前我得到了一个元素的数组,但我没有得到任何结果。有没有办法提取这个 link 这样我就不会返回一个数组?我用这个 tool 进行测试。
实现我所需要的代码如下所示:
JsonPathLinkDiscoverer disc = new JsonPathLinkDiscoverer("$._embedded..auth.._links..['%s']..href", MediaTypes.HAL_JSON);
Link authLink = disc.findLinksWithRel("auth:default", <json>).get(0);
其中 <json>
应该是原始问题中示例的 json 表示(可能类似于其他调用的响应)。
这是我试图与 Traverson 一起处理的 Json 以从此 HAL 中提取单个 link:
{
"_links": {
"curies": [
{
"href": "https://localhost/auth/def/rels/{rel}",
"name": "auth",
"templated": true
}
],
"self": {
"href": "https://localhost/auth/identity"
}
},
"_embedded": {
"auth": [
{
"_links": {
"auth:ad": [
{
"href": "https://localhost/auth/ad"
}
]
},
"kind": "oauth2"
},
{
"_links": {
"auth:default": [
{
"href": "https://localhost/auth/default" // **Link that I need**
}
]
},
"kind": "oauth2"
}
]
}
}
Java 理想情况下负责处理的代码应该如下所示:
Link test = traverson
.follow("auth")
.follow("$.['_embedded']['auth']..['_links']['auth:default']..['href'][0]")
.asLink();
但是由于某种原因,尽管在 JSONPath 表达式中添加最后一个 [0]
之前我得到了一个元素的数组,但我没有得到任何结果。有没有办法提取这个 link 这样我就不会返回一个数组?我用这个 tool 进行测试。
实现我所需要的代码如下所示:
JsonPathLinkDiscoverer disc = new JsonPathLinkDiscoverer("$._embedded..auth.._links..['%s']..href", MediaTypes.HAL_JSON);
Link authLink = disc.findLinksWithRel("auth:default", <json>).get(0);
其中 <json>
应该是原始问题中示例的 json 表示(可能类似于其他调用的响应)。