带有大字符串的json_encode

时间:2013-01-31 09:50:58

标签: php

我在json对象字段$o->sHtml中返回一个html响应。

测试sHtml大约是13000个字符,json_encode后是$o->sHtml = null。 知道为什么吗?

1 个答案:

答案 0 :(得分:4)

Json编码仅适用于UTF-8编码数据。检查输入数据是否为utf8

$json  = json_encode($o->sHtml); //or json_encode($o);
$error = json_last_error();
var_dump($json, $error === JSON_ERROR_UTF8);

这些是可能的错误

JSON_ERROR_NONE -   No error has occurred    
JSON_ERROR_DEPTH -  The maximum stack depth has been exceeded    
JSON_ERROR_STATE_MISMATCH - Invalid or malformed JSON    
JSON_ERROR_CTRL_CHAR -Control character error, possibly incorrectly encoded  
JSON_ERROR_SYNTAX - Syntax error     
JSON_ERROR_UTF8 -   Malformed UTF-8 characters, possibly incorrectly encoded

参考:http://www.php.net/manual/en/function.json-last-error.php