开发者

PHP::How to convert json object into array if json value contains encoded data?

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.


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 http://www.freeimagehosting.net/uploads/d9b635a0a0.gif


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"]);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜