Php JSON编码和解码
我有这样一个数组:Php JSON编码和解码,php,json,Php,Json,我有这样一个数组: 数组( [utm_source]=>网站 [utm_介质]=>fbshare [utm_活动]=>camp1 [测试类别]=>红色 [test_sub]=>类别 [测试参考]=>RJDEP ) 我把它编码成一个cookie。我从cookie中获取它,现在想解码它,但是我得到一个空白屏幕。我搞不清楚到底出了什么问题。在我看来,这个JSON看起来是正确的: {"utm_source":"website","utm_medium":"fbshare","utm_campaign"
数组(
[utm_source]=>网站
[utm_介质]=>fbshare
[utm_活动]=>camp1
[测试类别]=>红色
[test_sub]=>类别
[测试参考]=>RJDEP
)
我把它编码成一个cookie。我从cookie中获取它,现在想解码它,但是我得到一个空白屏幕。我搞不清楚到底出了什么问题。在我看来,这个JSON看起来是正确的:
{"utm_source":"website","utm_medium":"fbshare","utm_campaign":"camp1","test_cat":"red","test_sub":"Category","test_ref":"dodere"}
有什么想法吗
编辑:
我的代码:
$value = array(
'utm_source' => 'website',
'utm_medium' => 'fbshare',
'utm_campaign' => 'camp1',
'test_cat' => 'red',
'test_sub' => 'Category',
'test_ref' => 'rjdepe'
);
$value = json_encode($value);
setcookie("TestCookie", $value, time()+3600);
其他网页:
$cookie = $_COOKIE['TestCookie'];
$cookie = json_decode($cookie);
print_r($cookie);
$cookie = $_COOKIE['TestCookie'];
$cookie = json_decode(base64_decode($cookie));
print_r($cookie);
在申请前:
print_r($cookie);
做:
它还回什么吗?如果出现一个空白屏幕,可能是因为解析器失败,很可能是cookie中json字符串中的“
”被转义的结果。
尝试:
更新
因此,我使用以下代码,并收到以下输出:
$value = array(
'utm_source' => 'website',
'utm_medium' => 'fbshare',
'utm_campaign' => 'camp1',
'test_cat' => 'red',
'test_sub' => 'Category',
'test_ref' => 'rjdepe'
);
var_dump($value);
setcookie('TestCookie', json_encode($value), time()+86400);
echo $_COOKIE['TestCookie'];
print_r(json_decode($_COOKIE['TestCookie']));
输出
array(6) {
["utm_source"]=>
string(7) "website"
["utm_medium"]=>
string(7) "fbshare"
["utm_campaign"]=>
string(5) "camp1"
["test_cat"]=>
string(3) "red"
["test_sub"]=>
string(8) "Category"
["test_ref"]=>
string(6) "rjdepe"
}
{
"utm_source":"website",
"utm_medium":"fbshare",
"utm_campaign":"camp1",
"test_cat":"red",
"test_sub":"Category",
"test_ref":"rjdepe"
}
stdClass Object
(
[utm_source] => website
[utm_medium] => fbshare
[utm_campaign] => camp1
[test_cat] => red
[test_sub] => Category
[test_ref] => rjdepe
)
如果您注意到,encoded是一个数组。json字符串是一个字符串。解码的字符串是一个对象
可以将此类型强制转换为数组:
$value = (array) json_decode($_COOKIE['TestCookie']);
// Or
$value = json_decode($_COOKIE['TestCookie'], true);
而且
根据您的配置,PHP可能会转义cookie中的特殊字符,这似乎就是JSON解码错误所传递的内容
试着做:
json_decode(str_replace('\"', '"', $_COOKIE['TestCookie']), true);
尝试按如下方式对其进行base64_编码:
$value = array(
'utm_source' => 'website',
'utm_medium' => 'fbshare',
'utm_campaign' => 'camp1',
'test_cat' => 'red',
'test_sub' => 'Category',
'test_ref' => 'rjdepe'
);
$value = base64_encode(json_encode($value));
setcookie("TestCookie", $value, time()+3600);
其他网页:
$cookie = $_COOKIE['TestCookie'];
$cookie = json_decode($cookie);
print_r($cookie);
$cookie = $_COOKIE['TestCookie'];
$cookie = json_decode(base64_decode($cookie));
print_r($cookie);
我们怎么才能猜出你的确切代码?@dqlopez:这是
print\r
的结果,你能不能加上一点你是如何做到这一点的?此外,您的web服务器日志是否显示任何相关信息?请确保将“错误报告”设置为足够高的值,以便正确报告它,并可能确保“显示错误”设置为true。@dqlopez这是print\r
的结果:oI在验证器上尝试了JSON,它确实是有效的JSON。当我执行JSON\u last\u error()时,我得到了4=JSON\u error\u语法;我尝试了你给我的东西,但是strip\u slashes
不起作用。你是说stripslashes
?我尝试在数据不是cookie时解码,它似乎解码得很好,但如果我从cookie中提取它,它就不起作用。