【发布时间】:2015-03-16 22:50:22
【问题描述】:
我正在尝试从 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
【问题讨论】:
-
\"使您的 JSON 无效 -
@kingkero 这就是 print_r() 的打印内容。我无权访问创建 cookie 的系统,但简而言之,它在数组上运行 json_encode() 并将该字符串设置为 cookie 的值。
标签: php arrays json cookies encode