views:

293

answers:

2

Hi, I am trying to convert json data in to array using php. But unfortunately my json data contains encoded values. For this reason why i am unable to convert this json object in to array in php. I tried it in different ways, But i couldn't get any solutions to solve this.

This is the URL data i am getting from client side.

controller.php?type=assignPacksToServer&sid=54&skey=j59fsWqaBw!Gh!KoTbhC&svid=268&packs=[{"packid":"22","pverid":"18","yaml":"-%20url%3A%20%2Fstatic%0A%20%20static_dir%3A%20static%0A%0A-%20url%3A%20.*%0A%20%20script%3A%20provider.py"},{"packid":"23","pverid":"19","yaml":"-%20url%3A%20%2Fstatic%0A%20%20static_dir%3A%20static%0A%0A-%20url%3A%20.*%0A%20%20script%3A%20provider.py"}]

PHP Code:

$packs = json_decode(urldecode($_POST["packs"]),true);
print_r($packs);

Unfortunately it dosen't print anying. If i send null packs data, then it prints ok. Any help greatly appreciated.

Thanking you, sureace.

A: 

Your php file is the controller.php ? Try your commands without urldecode. The decoding is already done when your script is called.

So just json_decode($_POST["yourparameter"]);

Alex
Yes, controller.php is my file. I tried as you said, but still it dosen't print anything.Tried code-------------$packs = json_decode($post["packs"],true);print_r($packs);
@user400091: You have a spelling mistake there, it should be:`$packs = json_decode($_REQUEST["packs"],true); print_r($packs);`
Alex
+1  A: 

To begin with, if it is from the URL, then it is a GET, not a POST. Plus you need to do it the other way around:

$packs = json_decode($_GET["packs"],true);
foreach($packs as $n => $value)
    if (isset($value['yaml']))
        $packs[$n]['yaml'] = urldecode($value['yaml']);
print_r($packs);

Hope that helps.

Edit: Screenshot to show that the snippet of code works perfectly:

alt text

Blizz
When i am trying using your code. It throws error like this Warning</b>: Invalid argument supplied for foreach() in....Please check with my urldata and give a solution.
I am trying with this code.$obj = stripslashes( $get['packs'] );$obj = json_decode($obj, true);print_r($obj);But it prints look like this instead of array print format[{"packid":"22","pverid":"18","yaml":"- url: /static static_dir: static- url: .* script: provider.py"},{"packid":"23","pverid":"19","yaml":"- url: /static static_dir: static- url: .* script: provider.py"}]{"status":"ok","msg":"Service package\/zetaservices configuration saved successfully."}Thanking you
The code I gave you works perfectly for the output you added in your initial question (trust me, I tested it). It assumes that $_GET['packs'] actually has as contents the string behind 'packs='The stripslashes here is really not needed, unless you have the magic_quotes enabled, but given your original paste I don't think that is the case. You first need to do the json decode, to turn the entire thing into an array and then do further decoding on the values in the array, not the other way around.
Blizz
When i am trying with this $packs = json_decode($_GET["packs"],true); It returns string instead of array. print_r() prints only json string. When i am testing the return value($packs) using with is_string() and is_array(). It says is_string() - true and is_array() is false.I am very hesitated to solve this problem. If you are getting correct output. Please paste the code here. I will try that in my system.
Then you must not be giving us all the information. As I said: I wrote the snippet in my response using the variables you gave. Only thing I did was cut the part behind packs= out of the url and assign it to another variable. Look at my update in the post.
Blizz