Arrays 无法在perl中读取json文本:将数组强制转换为哈希时索引错误
我将json字符串设置为:Arrays 无法在perl中读取json文本:将数组强制转换为哈希时索引错误,arrays,json,perl,Arrays,Json,Perl,我将json字符串设置为: [{"oslc_cm:totalCount":1,"oslc_cm:results":[{"rtc_cm:photo":null,"rtc_cm:modifiedBy":{"rdf:resource":"https:\/\/xyz.com\/jts\/users\/abc"},"dc:modified":"2014-03-27T11:25:55.504Z","rdf:resource":"https:\/\/xyz.com\/jts\/users\/user","rt
[{"oslc_cm:totalCount":1,"oslc_cm:results":[{"rtc_cm:photo":null,"rtc_cm:modifiedBy":{"rdf:resource":"https:\/\/xyz.com\/jts\/users\/abc"},"dc:modified":"2014-03-27T11:25:55.504Z","rdf:resource":"https:\/\/xyz.com\/jts\/users\/user","rtc_cm:userId":"id","rtc_cm:emailAddress":"mailto:abc%40xyz.com","dc:title":"JSON Editor"}]}]
正在尝试读取标记处的值dc:title
但是得到的错误是
将数组强制转换为哈希时索引错误
我的代码片段如下所示:
my $json_obj = $json->decode($json_text);
foreach my $item( @{$json_obj} ){
$WICommentCreator = $item->{'oslc_cm:results'}->{'dc:title'};
}
欢迎回答试试看
$WICommentCreator = $item->{'oslc_cm:results'}[0]{'dc:title'};
而不是
$WICommentCreator = $item->{'oslc_cm:results'}->{'dc:title'};
asoslc\u cm:results
是对象/散列的数组,而不是散列本身
[
{
"oslc_cm:totalCount": 1,
"oslc_cm:results": [ # array here
{ # first element of array
"rtc_cm:photo": null,
"rtc_cm:modifiedBy": {
"rdf:resource": "https://xyz.com/jts/users/abc"
},
"dc:modified": "2014-03-27T11:25:55.504Z",
"rdf:resource": "https://xyz.com/jts/users/user",
"rtc_cm:userId": "id",
"rtc_cm:emailAddress": "mailto:abc%40xyz.com",
"dc:title": "JSON Editor" # wanted key/value
}
]
}
]
我的意思是,当数组大小不只是0时,我如何使它成为动态的。@user3616128在
{$item->{'oslc\u cm:results'}}
数组上循环,就像在{$json\u obj}
@user3616128上循环一样,如果这个答案对您有效,您可以接受它。