我真的很努力去理解如何瀏覽一個JSON對象。我正在使用JSON.pm,並且我正在試圖訪問每個「名稱」參數並將其「values」數組的第一個索引存儲在我的桌面上。如何從PERL中提取此JSON對象的數據?
我無法理解基本結構,數組/哈希引用是如何工作的,以及如何遍歷它們以訪問我需要的值。如果有人能幫助我指出正確的方向......我現在很迷茫/困惑。
這是我正在使用的示例數據(Facebook開放圖形對象)。
{
"data": [
{
"id": "219127134886593/insights/page_impressions/week",
"name": "page_impressions",
"period": "week",
"values": [
{
"value": 31600,
"end_time": "2013-03-07T08:00:00+0000"
},
{
"value": 31979,
"end_time": "2013-03-08T08:00:00+0000"
},
{
"value": 29517,
"end_time": "2013-03-09T08:00:00+0000"
}
],
"title": "Weekly Total Impressions",
"description": "Weekly The number of impressions seen of any content associated with your Page. (Total Count)"
},
{
"id": "219127134886593/insights/page_impressions_organic/week",
"name": "page_impressions_organic",
"period": "week",
"values": [
{
"value": 23587,
"end_time": "2013-03-07T08:00:00+0000"
},
{
"value": 23858,
"end_time": "2013-03-08T08:00:00+0000"
},
{
"value": 22813,
"end_time": "2013-03-09T08:00:00+0000"
}
],
"title": "Weekly Organic impressions",
"description": "Weekly The number of times your posts were seen in News Feed or ticker or on visits to your Page. These impressions can be by people who have liked your Page and people who haven't. (Total Count)"
},
],
"paging": {
"previous": "https://graph.facebook.com/219127134886593/insights/page_impressions,page_impressions_organic,page_impressions_viral,page_storytellers/week?since=1362383320&until=1362642520",
"next": "https://graph.facebook.com/219127134886593/insights/page_impressions,page_impressions_organic,page_impressions_viral,page_storytellers/week?since=1362901720&until=1363160920"
}
}
而這就是我的工作訪問不同的元素:
foreach my $data ($decoded_json->{data}) {
foreach my $item (@$data) {
$list{ $item->{'name'} } = $item->{'values'};
print "Value 3: @($item->{'values'})[3]\n";
}
}
_「我無法理解的基本結構,如何數組/哈希引用工作,如何迭代它們以訪問我需要的值。「 - 那麼你應該參考一個初學者教程,該教程解釋了基本的PERL數據類型以及如何使用它們。 – CBroe 2013-03-12 09:16:55
我明白了,我是。但是,在使用JSON對象時它不會點擊。我希望通過一個例子來幫助我理解。 – 2013-03-12 09:26:01
沒有「JSON對象」這樣的東西。 JSON是一種文本格式。有關Perl中的參考,請參見['perldoc perlreftut'](http://perldoc.perl.org/perlreftut.html)。 – melpomene 2013-03-12 10:03:54