键中点时的 JSONPath 语法
JSONPath Syntax when dot in key
用词不当请见谅,我是菜鸟
我有一些简单的JSON:
{
"properties": {
"footer.navigationLinks": {
"group": "layout"
, "default": [
{
"text": "Link a"
, "href": "#"
}
]
}
}
}
我正在尝试查明“footer.navigationLinks”,但我在使用键名中的点时遇到了问题。我正在使用 http://jsonpath.com/,当我输入
$.properties['footer.navigationLinks']
我得到 'No match'。如果我将密钥更改为“footernavigationLinks”,它可以工作,但我无法控制 JSON 文件中的密钥名称。
有人可以帮我定位那个键名吗?
此问题已于 2007 年报告为 issue #4 - Member names containing dot fail 并已修复。
修复不存在于此在线jsonpath.com implementation, but it is fixed in this old archive and probably in most of the forks that have been created since (like here and here)。
有关错误的详细信息
有缺陷的代码与 2007 年更正版本的代码之间的比较表明,更正是在私有 normalize
函数中进行的。
在 2007 年更正后的版本中,它显示为:
normalize: function(expr) {
var subx = [];
return expr.replace(/[\['](\??\(.*?\))[\]']|\['(.*?)'\]/g, function([=10=],,){
return "[#"+(subx.push(||)-1)+"]";
}) /* http://code.google.com/p/jsonpath/issues/detail?id=4 */
.replace(/'?\.'?|\['?/g, ";")
.replace(/;;;|;;/g, ";..;")
.replace(/;$|'?\]|'$/g, "")
.replace(/#([0-9]+)/g, function([=10=],){
return subx[];
});
},
该序列中的第一个和最后一个 replace
确保第二个 replace
不会将 属性 名称中的点解释为 属性 分隔符。
我查看了从那时起制作的最新分叉,代码自那以后有了巨大的发展。
结论:
jsonpath.com 基于 JSONPath 的过时版本,无法可靠地预览当前库将为您提供的内容。
有 json 回复:
{
"0": {
"SKU": "somevalue",
"Merchant.Id": 234
}
}
我可以使用 . (点)在名称中。
jsonPath.getJsonObject("0.\"Merchant.Id\"")
注意:引号和它们被转义的事实。
注意 不确定其他版本,但我正在使用
'com.jayway.restassured', name: 'json-path', version: '2.9.0'
我见过一些 samples/solutions 使用带括号的单引号,但对我不起作用。
有关信息,jsonpath.com 自问题提出以来已被修补,现在它适用于问题中给出的示例。我成功地尝试了这些路径:
$.properties['footer.navigationLinks']
$.properties.[footer.navigationLinks]
$.properties.['footer.navigationLinks']
$['properties']['footer.navigationLinks']
$.['properties'].['footer.navigationLinks']
properties.['footer.navigationLinks']
- 等等
你可以把'key with dots'用单引号括起来如下
response.jsonpath().get("properties.'footer.navigationLinks'")
或者甚至转义单引号,如下所示:
response.jsonpath().get("properties.\'footer.navigationLinks\'")
两者都很好
用词不当请见谅,我是菜鸟
我有一些简单的JSON:
{
"properties": {
"footer.navigationLinks": {
"group": "layout"
, "default": [
{
"text": "Link a"
, "href": "#"
}
]
}
}
}
我正在尝试查明“footer.navigationLinks”,但我在使用键名中的点时遇到了问题。我正在使用 http://jsonpath.com/,当我输入
$.properties['footer.navigationLinks']
我得到 'No match'。如果我将密钥更改为“footernavigationLinks”,它可以工作,但我无法控制 JSON 文件中的密钥名称。
有人可以帮我定位那个键名吗?
此问题已于 2007 年报告为 issue #4 - Member names containing dot fail 并已修复。
修复不存在于此在线jsonpath.com implementation, but it is fixed in this old archive and probably in most of the forks that have been created since (like here and here)。
有关错误的详细信息
有缺陷的代码与 2007 年更正版本的代码之间的比较表明,更正是在私有 normalize
函数中进行的。
在 2007 年更正后的版本中,它显示为:
normalize: function(expr) {
var subx = [];
return expr.replace(/[\['](\??\(.*?\))[\]']|\['(.*?)'\]/g, function([=10=],,){
return "[#"+(subx.push(||)-1)+"]";
}) /* http://code.google.com/p/jsonpath/issues/detail?id=4 */
.replace(/'?\.'?|\['?/g, ";")
.replace(/;;;|;;/g, ";..;")
.replace(/;$|'?\]|'$/g, "")
.replace(/#([0-9]+)/g, function([=10=],){
return subx[];
});
},
该序列中的第一个和最后一个 replace
确保第二个 replace
不会将 属性 名称中的点解释为 属性 分隔符。
我查看了从那时起制作的最新分叉,代码自那以后有了巨大的发展。
结论:
jsonpath.com 基于 JSONPath 的过时版本,无法可靠地预览当前库将为您提供的内容。
有 json 回复:
{
"0": {
"SKU": "somevalue",
"Merchant.Id": 234
}
}
我可以使用 . (点)在名称中。
jsonPath.getJsonObject("0.\"Merchant.Id\"")
注意:引号和它们被转义的事实。
注意 不确定其他版本,但我正在使用
'com.jayway.restassured', name: 'json-path', version: '2.9.0'
我见过一些 samples/solutions 使用带括号的单引号,但对我不起作用。
有关信息,jsonpath.com 自问题提出以来已被修补,现在它适用于问题中给出的示例。我成功地尝试了这些路径:
$.properties['footer.navigationLinks']
$.properties.[footer.navigationLinks]
$.properties.['footer.navigationLinks']
$['properties']['footer.navigationLinks']
$.['properties'].['footer.navigationLinks']
properties.['footer.navigationLinks']
- 等等
你可以把'key with dots'用单引号括起来如下
response.jsonpath().get("properties.'footer.navigationLinks'")
或者甚至转义单引号,如下所示:
response.jsonpath().get("properties.\'footer.navigationLinks\'")
两者都很好