harryhalpin has asked for the wisdom of the Perl Monks concerning the following question:
I can convert my data from JSON to objects using the Perl JSON module (http://search.cpan.org/dist/JSON/), but I am having difficulty accessing the data in perl objects. The data appears to be in an array of hashes, but I cannot use any object accessor functions to get at the data. Here's a sample of the data:
Here's a snippet of my code. Note $inventors_obj holds the data.{ "items" : [ { "name" : "Theodor Nelson", "id": "_333301" }, { "name": "Morton Heilig", "id": "_13204" } ] }
But $inventors_obj->{"items} appears to be returning a single object, rather than an array. How can iterate through it, change things, and print it back to JSON?$inventors_obj = jsonToObj($inventors_json[0]); #I want to iterate through my list of inventors @inventors_array = $inventors_obj->{"items"};
However, if I convert the object to an array of hashes (how do I do this, "unblessing"?), can I get it back out in JSON? Is there a way I can manipulate it while keeping it as an object?#print name doesn't work, because the entire object is #stored at $inv +entors_array[0]. print $inventors_array[0]->{"name"} #So this doesn't work: print $inventors_array[1]->{"name"} #I am not sure if this is possible to even alter data #generically wit +h objects, even though I thought they were #really just arrays of hashes: $inventors_array[1]->{"name"} = "New Name"; #But I can do with an array of hashes... $inventors_array[1]{"name"} = "New Name";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: JSON and Perl Objects - How to access data?
by jZed (Prior) on Nov 18, 2007 at 22:42 UTC | |
by Anonymous Monk on Jun 02, 2010 at 18:52 UTC | |
by Anonymous Monk on Sep 05, 2015 at 15:57 UTC | |
by 1nickt (Canon) on Sep 05, 2015 at 17:40 UTC | |
by Anonymous Monk on Sep 06, 2015 at 04:35 UTC | |
by AnomalousMonk (Archbishop) on Sep 06, 2015 at 13:04 UTC | |
by Anonymous Monk on May 25, 2018 at 00:37 UTC | |
by poj (Abbot) on Sep 05, 2015 at 16:39 UTC | |
Re: JSON and Perl Objects - How to access data?
by stvn (Monsignor) on Nov 19, 2007 at 04:00 UTC |
Back to
Seekers of Perl Wisdom