gpt4 book ai didi

jsonpath - 访问 JSON::Path numbers only key

转载 作者:行者123 更新时间:2023-12-04 11:33:17 25 4
gpt4 key购买 nike

使用 Perl6 Module JSON::Path 访问只有数字的 json 键的正确语法是什么?我收到“位置 6 处的 JSON 路径解析错误”错误。

我想访问项目->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);

什么都不返回
#()
$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 版中解决。

关于jsonpath - 访问 JSON::Path numbers only key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55733492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com