开发者

json_decode - malformed JSON from AJAX Request

I am building a request in Flash开发者_开发知识库 that contains a JSON encoded Array of Objects. This is encoded using as3corelib. The request is then passed to JavaScript via ExternalInterface and a jquery ajax call sends the request off to the server.

In PHP, the incoming data is parsed, but the json_decode returns null, giving a Malformed JSON error. var_dump results in comments:

<?php
    (isset($_POST['gdata']) && !empty($_POST['gdata'])) ? $gamedata = $_POST['gdata'] : returnError("game data not specified");
    var_dump($gamedata);                             // (String) = string(37) "[{\"duration\":1,\"id\":\"game2\"}]"
    $gamedata = json_decode(utf8_encode(trim($gamedata)),true);
    var_dump($gamedata);                             // null
    $gamedata = json_decode("[{\"duration\":1,\"id\":\"game2\"}]",true);
    var_dump($gamedata);
    /*
    array(1) {
      [0]=>
      object(stdClass)#1 (2) {
        ["duration"]=>
        int(1)
        ["id"]=>
        string(7) "game2"
      }
    }
    */
?>

What I don't understand is that attempting to decode the variable returns null, but the same text decoded from a literal string works fine. What can I do to clean up the incoming data and make it readable for json_decode?

Edit: php_info() says that magic_quotes_gpc is enabled. Could that be the issue?


magic_quotes_gpc could be the issue, yes. And if you re-encode blindly w/o knowing the charset could be an issue as well.

So if you know magic_quotes_gpc is enabled, you need to strip slashes first.

For the charset, take care you know in which charset the incomming data is encoded, not that it's already utf-8 encoded and you assume it's latin-1 and convert it again.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜