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:
{ "items" : [ { "name" : "Theodor Nelson", "id": "_333301" }, { "name": "Morton Heilig", "id": "_13204" } ] }
Here's a snippet of my code. Note $inventors_obj holds the data.
$inventors_obj = jsonToObj($inventors_json[0]); #I want to iterate through my list of inventors @inventors_array = $inventors_obj->{"items"};
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?
#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";
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?

In reply to JSON and Perl Objects - How to access data? by harryhalpin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.