访问JSON :: Path number only key

问题描述 投票:7回答:1

访问只有具有Perl6模块JSON :: Path的数字的json密钥的正确语法是什么?我得到“位置6处的JSON路径解析错误”错误。

我想访问items-> 2018-> name:

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";
perl6 jsonpath
1个回答
8
投票

尝试的语法:

$jp .= new: ".items['2018'].name";
say $jp.values($json);

是正确的,但JSON::Path有一个错误。它已在模块的1.6版本中得到解决。

© www.soinside.com 2019 - 2024. All rights reserved.