访问 JSON::Path 个数字密钥
Access JSON::Path numbers only key
使用 Perl6 模块 JSON::Path 访问只有数字的 json 键的正确语法是什么?我收到 "JSON path parse error at position 6" 错误。
我想访问项目->2018->名称:
use JSON::Path;
my Str $json = 「
{
"items" : {
"old" : { "name" : "olditem" },
"2017" : { "name" : "item1" },
"2018" : { "name" : "item2" },
"2019" : { "name" : "item3" }
}
}
」;
没关系
#("olditem", "item3", "item1", "item2")
my JSON::Path $jp .= new: '.items[*].name';
say $jp.values($json);
也可以
#("olditem")
$jp .= new: '.items.old.name';
say $jp.values($json);
什么都不做return
#()
$jp .= new: ".items['2018'].name";
say $jp.values($json);
错误
#JSON path parse error at position 6
try {
$jp .= new: ".items.2018.name";
CATCH {
default { .Str.say }
}
}
错误还有:
#JSON path parse error at position 6
try {
$jp .= new: ".items.['2018'].name";
CATCH {
default { .Str.say }
}
}
完整的错误输出:
#`[
JSON path parse error at position 6
in method giveup at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 43
in regex commandtree at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 15
in regex commandtree at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 15
in regex TOP at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 11
in submethod TWEAK at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 205
in method new at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 200
in block <unit> at test4 line 42
]
$jp .= new: ".items.['2018'].name";
尝试的语法:
$jp .= new: ".items['2018'].name";
say $jp.values($json);
是正确的,但是 JSON::Path
中有一个错误。已在模块的 1.6 版本中解决。
使用 Perl6 模块 JSON::Path 访问只有数字的 json 键的正确语法是什么?我收到 "JSON path parse error at position 6" 错误。
我想访问项目->2018->名称:
use JSON::Path;
my Str $json = 「
{
"items" : {
"old" : { "name" : "olditem" },
"2017" : { "name" : "item1" },
"2018" : { "name" : "item2" },
"2019" : { "name" : "item3" }
}
}
」;
没关系
#("olditem", "item3", "item1", "item2")
my JSON::Path $jp .= new: '.items[*].name';
say $jp.values($json);
也可以
#("olditem")
$jp .= new: '.items.old.name';
say $jp.values($json);
什么都不做return
#()
$jp .= new: ".items['2018'].name";
say $jp.values($json);
错误
#JSON path parse error at position 6
try {
$jp .= new: ".items.2018.name";
CATCH {
default { .Str.say }
}
}
错误还有:
#JSON path parse error at position 6
try {
$jp .= new: ".items.['2018'].name";
CATCH {
default { .Str.say }
}
}
完整的错误输出:
#`[
JSON path parse error at position 6
in method giveup at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 43
in regex commandtree at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 15
in regex commandtree at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 15
in regex TOP at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 11
in submethod TWEAK at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 205
in method new at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 200
in block <unit> at test4 line 42
]
$jp .= new: ".items.['2018'].name";
尝试的语法:
$jp .= new: ".items['2018'].name";
say $jp.values($json);
是正确的,但是 JSON::Path
中有一个错误。已在模块的 1.6 版本中解决。