CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How can we retrieve encode_json value which is in encode_json associative array formate.

 



rubyperl
New User

Mar 5, 2011, 2:41 PM

Post #1 of 1 (269 views)
How can we retrieve encode_json value which is in encode_json associative array formate. Can't Post

HI all,

How can we retrieve encode_json value which is in encode_json associative array formate.

my code is as follows to doing that.


%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\"}}}}"}';

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

$dec_json_obj = decode_json $temp;
$temp=$dec_json_obj->{REQUEST}; #hash form

$dec_json_obj=&get_hash_to_str($temp);

$temp=$dec_json_obj->{Items};

$dec_json_obj=&get_hash_to_str($temp);



$temp=$dec_json_obj->{0};
$dec_json_obj=&get_hash_to_str($temp);
print $temp=$dec_json_obj->{ItemName};


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


Here i am sending values like these to retrieve 0 th element of item
and get ItemName

$temp=$dec_json_obj->{0};
$dec_json_obj=&get_hash_to_str($temp);
print $temp=$dec_json_obj->{ItemName};


I want to get all the values of 0 th 1 st 2 nd .... ItemName values using loop.
Because it is not feasible to pass single -single values like 0,1,2...

Thanks...

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives