2015-03-02 66 views
0

我有這樣的數據,以數據表單元格:AlgoVital Plus čćš所以一些字符是utf-8PHP PDO - Json的編碼與UTF-8

這裏是我的數據庫架構

enter image description here

之後,我做JSON用PHP文件,並將其發送給主音:

//header 
header('Content-Type: text/html; charset=utf-8'); 
... 
... 
//at the end of php file: 
$jsonTable = json_encode($table); 
    //echo $jsonTable; 
    } catch(PDOException $e) { 
     echo 'ERROR: ' . $e->getMessage(); 
    } 
    echo $jsonTable; 

和HTML頭也沒關係:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <meta charset="utf-8"> 

但我不能用utf-8字符顯示數據...爲什麼?

JSON不使用UTF-8 characers ......等等,我有一個像čćšž一些字符,然後我就在JSON文件中獲取NULL格式化,當我使用JSON編碼

我該如何解決這個問題?

+0

的可能重複(http://stackoverflow.com/questions/1972006/json- [JSON \ _encode被返回NULL?] encode-is-returning-null) – Mike 2015-03-02 18:22:56

+0

http://i.imgur.com/oX6LIlt.png – 2015-03-02 18:37:52

+0

當內容是application/json時,爲什麼要發送text/html內容類型頭? – 2015-03-02 18:39:42

回答

0

嘗試這樣的:

$table = utf8_encode($table); 
$jsonTable = json_encode($table); 
+0

不,當我寫$ table ['data'] = utf8_encode($ rows);我得到沒有數據的JSON – 2015-03-02 18:36:13

+0

我複製了你的代碼。所以它適用於我: $ array = Array(); $ array [「test」] =「AlgoVital Plusčćš」; $ jsonTable = json_encode($ array); echo $ jsonTable; – zamarrowski 2015-03-02 18:42:02

+0

@zamarrowski這意味着你不需要調用'utf_encode'? – 2015-03-02 18:43:31

0

試試這個

$table = '[{name: "PHP",version: 5.6}]'; //your json data 
$jsonTable = json_encode($table, JSON_UNESCAPED_UNICODE); 
$myObj = json_decode($jsonTable); 
echo $myObj->name;