【发布时间】:2021-05-23 21:25:18
【问题描述】:
我有一个 json 结构加载到散列中,如下所示:
my $data = {
"store1" => {
"book" => [
{ "category" => "reference",
"author" => "Nigel Rees",
"title" => "Sayings of the Century",
"price" => 8.95,
},
{ "category" => "fiction",
"author" => "Herman Melville",
"title" => "Moby Dick",
"isbn" => "0-553-21311-3",
"price" => 8.99,
},
],
"bicycle" => [
{ "color" => "red",
"price" => 19.95,
},
],
},
"store2" => {
"book" => [
{ "category" => "reference",
"author" => "Nigel Rees",
"title" => "Sayings of the Century",
"price" => 8.95,
},
{ "category" => "fiction",
"author" => "Herman Melville",
"title" => "Moby Dick",
"isbn" => "0-553-21311-3",
"price" => 8.99,
},
],
"bicycle" => [
{ "color" => "red",
"price" => 19.95,
},
],
},
};
像 JSON::Path 这样的工具,返回匹配路径的元素的值。
my $jpath = JSON::Path->new('$.*.book[*]');
my @books = $jpath->values($data);
我想获取路径,而不是值。比如:
[
'$->{store}{book}[0]',
'$->{store}{book}[1]',
'$->{store}{book}[2]'
]
我想在 Path 中查找信息,以便知道结果的位置而不是结果本身。 例如,给定查询:
my $jpath = JSON::Path->new('$.*.book[*]');
我想知道路径是否包含“store1”或“store2”,所以我需要查询来返回路径。
有没有办法做到这一点?
谢谢, 螺旋体
【问题讨论】: