Emma Emma - 1 year ago 111
Perl Question

decode list of JSON objects to array of hashes in perl

I'm a newbie to Perl,I want to parse the following JSON into array of hashes,(map method will be preferred)

{ "name" : "Theodor Nelson",
"id": "_333301",
"address": "Hello_world"
{ "name": "Morton Heilig",
"id": "_13204",
"address": "Welcome"

then wants to print only "




's values in foreach loop.
any kind of help will be appreciated.

Answer Source

you can simply do like

use JSON qw(encode_json decode_json);

my $JSON = [{   "name" : "Theodor Nelson",
        "id": "_333301",
        "address": "Hello_world },
        {   "name": "Morton Heilig",
        "id": "_13204",
        "address": "Welcome"}]

my $decoded = decode_json $JSON;

return template 'yourtemplate', {
    options =>  $decoded,

and in view file you can access it as option.id and option.name or option.address or whatever is required in FOREACH loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download