Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-09-23 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (212 votes), past polls