在perl中解码JSON

时间:2017-03-09 08:02:09

标签: json perl

我正在尝试在perl中解码一个简单的JSON字符串,但它不符合我习惯的JSON。

JSON适用于Browshot,看起来像:

{
  "52967":{
    "priority":"1",
    "status":"finished",
    "screenshot_url":"https://www.browshot.com/screenshot/image/52967?scale=1&key=MY_KEY",
    "id":"52967",
    "url": "http://www.google.com/",
    "size": "screen",
    "width": "1024",
    "height": "768",
    "request_time": "1312106111",
    "started": "1312258803994",
    "load": "1312258829461",
    "content": "1312258829661",
    "finished": "1312258829681",
    "instance_id": "10",
    "response_code": "200",
    "final_url": "http://www.google.com/",
    "content_type":"text/html",
    "scale": "1",
    "cost": "0",
    "referer": "",
    "post_data": "",
    "cookie": "",
    "delay": "5",
    "flash_delay": "10",
    "script": "",
    "shared_url": ""
  },
  "52969":{
    "priority":"1",
    "status":"finished",
    "screenshot_url":"https://www.browshot.com/screenshot/image/52969?scale=1&key=MY_KEY",
    "id":"52969",
    "url": "http://www.google.org/",
    "size": "screen",
    "width": 1024,
    "height": "768",
    "request_time": "1312106111",
    "started": "1312258803994",
    "load": "1312258829461",
    "content": "1312258829661",
    "finished": "1312258829681",
    "instance_id": "10",
    "response_code": "200",
    "final_url": "http://www.google.org/",
    "content_type":"text/html",
    "scale": "1",
    "cost": "0",
    "referer": "",
    "post_data": "",
    "cookie": "",
    "delay": "5",
    "flash_delay": "10",
    "script": "",
    "shared_url": ""
  },
  "52971":{
    "priority":"1",
    "status":"processing",
    "screenshot_url":"https://www.browshot.com/screenshot/image/52971?scale=1&key=MY_KEY",
    "id":"52971",
    "url": "http://www.google.de/",
    "size": "screen",
    "width": "1024",
    "height": "768",
    "request_time": "1312106111",
    "started": "1312258803994",
    "load": "1312258829461",
    "content": "1312258829661",
    "finished": "1312258829681",
    "instance_id": "10",
    "response_code": "200",
    "final_url": "http://www.google.de/",
    "content_type":"text/html",
    "scale": "1",
    "cost": "0",
    "referer": "",
    "post_data": "",
    "cookie": "",
    "delay": "5",
    "flash_delay": "10",
    "script": "",
    "shared_url": ""
  },
  "52970":{
    "priority":"1",
    "status":"finished",
    "screenshot_url":"https://www.browshot.com/screenshot/image/52970?scale=1&key=MY_KEY",
    "id":"52970",
    "url": "http://www.google.fr/",
    "size": "screen",
    "width": "1024",
    "height": "768",
    "request_time": "1312106111",
    "started": "1312258803994",
    "load": "1312258829461",
    "content": "1312258829661",
    "finished": "1312258829681",
    "instance_id": "10",
    "response_code": "200",
    "final_url": "http://www.google.fr/",
    "content_type":"text/html",
    "scale": "1",
    "cost": "0",
    "referer": "",
    "post_data": "",
    "cookie": "",
    "delay": "5",
    "flash_delay": "10",
    "script": "",
    "shared_url": ""
  },
  "52968":{
    "priority":"1",
    "status":"finished",
    "screenshot_url":"https://www.browshot.com/screenshot/image/52968?scale=1&key=MY_KEY",
    "id":"52968",
    "url": "http://www.google.com/",
    "size": "screen",
    "width": "1024",
    "height": "768",
    "request_time": "1312106111",
    "started": "1312258803994",
    "load": "1312258829461",
    "content": "1312258829661",
    "finished": "1312258829681",
    "instance_id": "10",
    "response_code": "200",
    "final_url": "http://www.google.com/",
    "content_type":"text/html",
    "scale": "1",
    "cost": "0",
    "referer": "",
    "post_data": "",
    "cookie": "",
    "delay": "5",
    "flash_delay": "10",
    "script": "",
    "shared_url": ""
  }
}

通常我会使用:

my $records = decode_json($JSON);

my @screens = @{ $records };
foreach my $f ( @screens ) {
  print "$f->{'screenshot_url'}|\n";
}

但这显然不起作用。

我确信这很简单,但我必须在这里找不到标记,找不到解决方案,所有帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

要迭代哈希,通常会迭代keys返回的键。

for my $id (keys(%$records)) {
   my $record = $records->{$id};
   say $record->{screenshot_url};
}