2012-06-06 8 views
1

는이 같은 배열을 가지고있다. 쿠키에서 가져 와서 이제는 해독하고 싶지만 빈 화면이 나타납니다. 나는 무엇이 잘못되었는지 혼란 스럽다. 나에게 JSON이 올바른 것처럼 보입니다.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(); 

아무 것도 반환하지 않습니까? 빈 화면이 나타나면 파서가 작동하지 않아 쿠키 내의 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는 JSON 디코딩 오류가 무엇 것 같다 쿠키에 특수 문자를 탈출 할 수있다 중계. 일을

시도 :

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

json_last_error()를 할 때 4 = JSON_ERROR_SYNTAX를 얻었습니다; 네가 나에게 준 것을 시도했지만'strip_slashes'가 작동하지 않는다.'stripslashes'를 의미 했습니까? – cKendrick

+0

나는 그것이 쿠키가 아닐 때 데이터를 디코드하려고 시도했다. 그리고 디코드하는 것처럼 보이지만 쿠키에서 꺼내면 작동하지 않는다. – cKendrick

+0

내 답변 편집, 위 참조 –