2014-04-04 168 views
0

我在google和stackoverflow中搜索了很多,但是找不到解決方案。PHP json_decode返回null

我有以下的JSON從DB來:

{"nome":"Tést","curso":"Educação Infantil","serie":"1ª Etapa 1011"} 

正如你所看到的,也有未保存爲Unicode UTF8一些字符。 當我嘗試解碼這個json時,它返回null和json_last_error返回。

有沒有人有解決方案?

+0

錯誤5是'JSON_ERROR_UTF8'。我認爲你需要以其他方式格式化字符''(如果可能的話)。 – Padarom

+0

你使用哪個版本的php? –

+0

我使用的是PHP 5.3.3-7 –

回答

3

使用方法如下。與單引號一起使用;

$a = '{"nome":"Tést","curso":"Educação Infantil","serie":"1ª Etapa 1011"}'; 
var_dump(json_decode($a)); 

這裏的工作演示:codepad

如果不行,或者你可以使用iconv您stirng轉換爲UTF-8和解碼等;

<?php 
$a = '{"nome":"Tést","curso":"Educação Infantil","serie":"1ª Etapa 1011"}'; 
$a = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($a)); 
$json = json_decode($a); 
var_dump($json); 
?> 
+0

也適用於我! –

+1

不適合我 –

+0

@RakeshSharma怎麼樣?我提供了演示。不適合你的演示嗎? –

1

嘗試

$utfstr= mb_convert_encoding($str ,"UTF-8"); 
$output = json_decode($utfstr, true); 
+0

這工作完美!非常感謝! :) –

+0

你的歡迎:) –