I want to send  a associative array as a value in json array.



for example i create a array like.


%responce_array1=();

$responce_array1{'REQUEST'}{'Items'{0}{'ItemName'}='demoName0';
$responce_array1{'REQUEST'}{'Items'}{1}{'ItemName'}='demoName1';
$responce_array1{'REQUEST'}{'Items'}{2}{'ItemName'}='demoName2';


my $itemjson_ary1 = encode_json \%responce_array1;


and pass it to as a value of a another array like :-

%responce_array2=();

$responce_array2{'item_json_array'}=$itemjson_ary1;

and json encode it like :-

my $itemjson_ary2 = encode_json \%responce_array2;

it gives out put  as below json encoded array.:-


print $itemjson_ary2;

$jsonary='{"item_json_array":"{\"REQUEST\":{\"Items\":{\"1\":{\"ItemName\":\"demoName1\"},\"0\":{\"ItemName\":\"demoName0\"},\"2\":{\"ItemName\":\"demoName2\"}}}}"}';


Now the problem is  how can  i retrive "ItemName" value.

because for  example i  take  three values  like 0,1,2 but these can be  many 0,1,2,3,4...

so i want to retrieve all these value in a loop.or any  way to convert it in to array so we can get these array.

my whole code as follows.


use JSON;

%responce_array1=();
%responce_array2=();


$responce_array1{'REQUEST'}{'Items'{0}{'ItemName'}='demoName0';
$responce_array1{'REQUEST'}{'Items'}{1}{'ItemName'}='demoName1';
$responce_array1{'REQUEST'}{'Items'}{2}{'ItemName'}='demoName2';

my $itemjson_ary1 = encode_json \%responce_array1;

$responce_array2{'item_json_array'}=$itemjson_ary1;

my $itemjson_ary2 = encode_json \%responce_array2;

print $itemjson_ary2;

$jsonary='{"item_json_array":"{\"REQUEST\":{\"Items\":{\"1\":{\"ItemName\":\"demoName1\"},\"0\":{\"ItemName\":\"demoName0\"},\"2\":{\"ItemName\":\"demoName2\"}}}}"}';


#here  i am decode these json array to get "item_json_array" value .

$dec_json_obj = decode_json $jsonary;
$temp=$dec_json_obj->{item_json_array};



#here  i am decode these json array to get "REQUEST" value .

$dec_json_obj = decode_json $temp;
$temp=$dec_json_obj->{REQUEST}; #hash form
$dec_json_obj=&get_hash_to_str($temp);



#here  i am decode these json array to get "Items" value .
$temp=$dec_json_obj->{Items};
$dec_json_obj=&get_hash_to_str($temp);


#here  i am decode these json array to get "0" value .
$temp=$dec_json_obj->{0};
$dec_json_obj=&get_hash_to_str($temp);


#here  i am decode these json array to get "ItemName" value .
print $temp=$dec_json_obj->{ItemName};

#out put is demoName0

# its subroutine to convert hash to string

sub get_hash_to_str($temp)
{
my $temp=$_[0];
my $enc_json_req = encode_json $temp;
print"
";
print $enc_json_req;
print"
";
my $dec_json_obj = decode_json $enc_json_req;
return $dec_json_obj;
}


In reply to Re^2: How can we retrieve encode_json value which is in encode_json associative array formate. by Anonymous Monk
in thread How can we retrieve encode_json value which is in encode_json associative array formate. by deepakgate6

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.