PHP从Cookie解码JSON

tec*_*ant 4 php arrays cookies json encode

我正在尝试从cookie中解析一个JSON编码的字符串,当我对该字符串运行json_decode()时,它返回为null。这应该是一个简单的操作-我缺少什么?

/* Get */

    $cookie_exampleData = $_COOKIE['exmaple_data'];

    // Retrieves: '{\"FirstName\":\"Angus\",\"LastName\":\"MacGyver\",\"Email\":\"hello@email.com\",\"Phone\":\"8185555555\"}'

/* Decode */

    $cookie_exampleData_decoded = json_decode($cookie_exampleData);

/* Print */

    var_dump($cookie_exampleData_decoded);

    // Returns: NULL
Run Code Online (Sandbox Code Playgroud)

voo*_*417 8

在这种情况下,您需要删除转义的引号:

$cookie_exampleData = stripslashes($_COOKIE['exmaple_data']);
Run Code Online (Sandbox Code Playgroud)

参见反斜杠

  • @technopeasant“不应该解码处理这个”=>不,它不应该转义斜杠。它不是有效的 JSON。 (2认同)