无法解码json。这是一个我编码为json并存储为cookie的数组

时间:2013-09-26 16:05:35

标签: php json cookies

这是我直接回显cookie时显示的输出,然后尝试json_decode它。

$_COOKIE['mycookie'] = {\"table_name\":\"wp_customtable\",\"time\":\"2013-09-26 08:18:23\",\"post_id\":null,\"referer\":\"www.bing.com\",\"keywords\":\"350 rear main seal replacement 11\",\"full_referrer\":\"http:\\/\\/www.bing.com\\/search?q=350+rear+main+seal+replacement+11&go=&qs=n&form=QBRE&pq=350+rear+main+seal+replacement+11&sc=0-32&sp=-1&sk=&cvid=dc09d67a2d214c8394bd39f2b28cf6cc\",\"ip\":\"123.45.67.89\"}

$cookie = $_COOKIE['mycookie'];
$output = json_decode($cookie, true);

var_dump($cookie);

string(392) "{\"table_name\":\"wp_customtable\",\"time\":\"2013-09-26 08:18:23\",\"post_id\":null,\"referer\":\"www.bing.com\",\"keywords\":\"350 rear main seal replacement 11\",\"full_referrer\":\"http:\\/\\/www.bing.com\\/search?q=350+rear+main+seal+replacement+11&go=&qs=n&form=QBRE&pq=350+rear+main+seal+replacement+11&sc=0-32&sp=-1&sk=&cvid=dc09d67a2d214c8394bd39f2b28cf6cc\",\"ip\":\"123.45.67.89\"}

var_dump($output);
NULL

1 个答案:

答案 0 :(得分:2)

只需在引号前删除斜杠:

json_decode(str_replace('\"', '"', $cookie), true);

See fiddle