我有一个Json数组对象,我试图在perl中解码。 json数组看起来像这样:
[
{
"name": "a123",
"enroll": "12a123",
"cs": {
"year1": {
"status": {
"oldvalue": "pending",
"new value": "complete"
}
}
}
},
{
"name": "b123",
"enroll": "12b123",
"ecm": {
"year1": {
"flag": {
"oldvalue": "null",
"new value": "ok"
}
}
}
},
{
"name": "c123",
"enroll": "12c123",
"cs": {
"year1": {
"status": {
"oldvalue": "complete",
"new value": "run new"
}
}
}
}
]
我想找到Json文件中最后一次出现的{“status”} - > {“new value”}的值。这里的输出应该是“run new”。仅供参考:并非每个对象都存在所有字段。任何有关如何解析此数组的帮助将非常感激。
答案 0 :(得分:0)
我为此使用了一个模块 - CPAN有一个module called JSON:Parse,它可以做你想做的事情:
use JSON::Parse 'parse_json';
my $json = loadJsonString(); # Load the JSON string from somewhere
my $courses = parse_json($json);
my $lastCourse = $courses->[-1];
my $newValue = $lastCourse->{cs}{year1}{status}{"new value"};