|
|
| Do you know where your variables are? | |
| PerlMonks |
Re^3: How to use a json string with nested array and nested hash elements?by Anonymous Monk |
| on Feb 04, 2013 at 12:50 UTC ( #1016942=note: print w/ replies, xml ) | Need Help?? |
|
Well, as aitap mentioned above, @jsonarray = $decoded_json->{strarray} is wrong; it should be $jsonarray = $decoded_json->{strarray}. (It makes no sense to call ref() on an array either.) Anyway, since $strarray is an array reference, you access its elements with $strarray->[$index], and if you need the whole array (in the foreach), you need to write @$strarray. (With hashes, the equivalents are $somevar->{$key} and %$somevar, respectively.) Your structure is... well, let's tackle the strarray. It is an array of a single element, and that element contains a hash. And that is where you went wrong. I think you missed the arrays altogether. There's one under pick_location and the other one was under strarray. But let's try this in code:
It's not a bad idea to postfix your intermediate variables with _href or _aref (like I did for one variable) just to remember whether you are dealing with a hash reference or an array reference. I hope this helps. I spent quite a while pondering on how to access a complex data structure that a module spat out for me, but that was quite a while ago.
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||