JSON编码和解码 [英] JSON Encode and Decode

查看:89
本文介绍了JSON编码和解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数组:

I have an array like this:

Array (
    [utm_source] => website
    [utm_medium] => fbshare
    [utm_campaign] => camp1
    [test_cat] => red
    [test_sub] => Category
    [test_ref] => rjdepe
)

我将其json_encode放入Cookie中.我从cookie中获取它,现在想对其进行解码,但是出现黑屏.我对什么是错感到困惑.对我来说,这个JSON看起来是正确的:

which I json_encode and put into a cookie. I take it from the cookie and now want to decode it but I get a blank screen. I am confused as to what is wrong. To me this JSON looks correct:

{"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);

推荐答案

尝试像这样对base64_encoding:

Try base64_encoding it like such:

$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(base64_decode($cookie));
print_r($cookie);

这篇关于JSON编码和解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆