JSON编码和解码

3

我有一个这样的数组:

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

我使用了json_encode将数据保存到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);

@dqlopez:这是print_r的结果。 - zerkms
你能添加一段代码片段来展示你实际上是如何做的吗?此外,你的Web服务器日志中是否显示了任何相关信息?请确保将error_reporting设置为足够高的级别以正确报告错误,并确保将display_errors设置为true。 - Skudd
@dqlopez这是print_r的结果 :o - Andreas Wong
我在验证器上尝试了你的JSON,它确实是有效的JSON。 - user377628
添加了我的代码,你是说这是print_r的结果? - cKendrick
没关系,我是在告诉一个评论你的Array无效的家伙,实际上它是print_r的结果。 - Andreas Wong
2个回答

11

尝试像这样对其进行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(base64_decode($cookie));
print_r($cookie);

3

在进行以下操作之前:

print_r($cookie);

Do:

json_last_error();

它有返回值吗?如果你得到了一个空屏幕,可能是因为解析器失败了,很可能是在cookie内的json字符串中的"被转义成了\"。 尝试:

$cookie = json_decode(stripslashes($_COOKIE['TestCookie']));

更新

我使用了以下代码,并收到以下输出:

    $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);

当我执行json_last_error()时,我得到了4 = JSON_ERROR_SYNTAX。我尝试了你给我的东西,但是strip_slashes不起作用,你是不是想说stripslashes? - cKendrick
我尝试解码数据,但当它不是一个cookie时,似乎可以正常解码,但如果我从cookie中提取它,则无法工作。 - cKendrick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接