2012-06-06 100 views
1

我有一个这样的数组:JSON编码和解码

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

json_encode投入的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); 
+3

我们怎么去猜测你的** **确切的代码? – zerkms

+0

@dqlopez:这是'print_r'的结果 – zerkms

+0

显示你的代码是怎么做的? – xdazz

回答

7

尝试base64_encoding像这样:

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

您之前:

print_r($cookie); 

务必:

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 
) 

如果您注意,编码是一个数组。 json字符串是一个字符串。解码的字符串是一个对象。

您可以键入施放此为一个数组:

$value = (array) json_decode($_COOKIE['TestCookie']); 
// Or 
$value = json_decode($_COOKIE['TestCookie'], true); 

此外,

根据您的配置,PHP可能会在你的cookie,这似乎是你的JSON解码错误是什么特殊字符转义中继。

尝试做:

json_decode(str_replace('\"', '"', $_COOKIE['TestCookie']), true); 
+0

当我做json_last_error()时,我得到4 = JSON_ERROR_SYNTAX;我试过你给我的,但是'strip_slashes'不起作用,你的意思是'stripslashes'? – cKendrick

+0

我试图解码数据时,它不是一个cookie,它似乎解码很好,但如果我从它的cookie它不起作用。 – cKendrick

+0

编辑我的答案,见上文 –