Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: JSON and Perl Objects - How to access data?

by jZed (Prior)
on Nov 18, 2007 at 22:42 UTC ( #651548=note: print w/ replies, xml ) Need Help??

in reply to JSON and Perl Objects - How to access data?

The structure is a hashref that contains a single member with key "items" which consists of an arrayref that contains two hashrefs. So you have to dereference the arrayref to get its members something like this:

for my $item( @{$inventors_object->{items}} ){ print $item->{name} . "\n"; };
To change the data:
$inventors_object->{items}->[0}->{name} = 'new name';
To turn it back into a JSON string:
my $new_json = objToJson( $inventors_object );
update here is your mistake:
@inventors_array = $inventors_obj->{"items"};
You are trying to turn an arrayref into an array without dereferencing it. Instead you want:
@inventors_array = @{ $inventors_obj->{"items"} };

Comment on Re: JSON and Perl Objects - How to access data?
Select or Download Code
Replies are listed 'Best First'.
Re^2: JSON and Perl Objects - How to access data?
by Anonymous Monk on Jun 02, 2010 at 18:52 UTC
    Man... thanks a lot.. i am trying to find useful info abt JSON and perl and this is pretty much the only one with some practical information. I still have not understood it completely but my sincere thanks. can u explain it to me wat exactly happens when u convert from json to perl obj. im lost in all the hashref and arrayref you are talking abt. Is it the object's structure that you are talking abt? if you could enumerate i would be grateful. thanks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://651548]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2015-08-29 18:23 GMT
Find Nodes?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...

    Results (343 votes), past polls