PHP从cookie解码JSON

时间:2015-01-18 23:30:58

标签: php arrays json cookies 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

1 个答案:

答案 0 :(得分:4)

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

$cookie_exampleData = stripslashes($_COOKIE['exmaple_data']);

请参阅stripslashes