Parse JSON with PERL [duplicate]

2020-04-19 06:37发布

I'd like to parse a JSON string with PERL.
My JSON string;

{"to_id": 0, "message": "This is a sample", "message_id": 1000, "from_id": 999}


How to parse the JSON and return all objects and values.
Sample output;

to_id: 0
message: This is a sample
message_id: 1000
from_id: 999


No XML output and object and value in separate variables

Thanks in advance

1条回答
倾城 Initia
2楼-- · 2020-04-19 07:22

Use the JSON module. Here's an example of how to use it:

use strict;
use warnings;
use Data::Dumper;
use JSON;

my $str = q|{"to_id": 0, "message": "This is a sample", "message_id": 1000, "from_id": 999}|;

print Dumper from_json($str);

Output:

$VAR1 = {
          'from_id' => 999,
          'message_id' => 1000,
          'to_id' => 0,
          'message' => 'This is a sample'
        };
查看更多
登录 后发表回答