StPiere
StPiere

Reputation: 4243

json_encode with large strings

I'm returning a html response in a json object field $o->sHtml.

The testing sHtml is about 13000 characters and after json_encode its $o->sHtml = null. Any idea why?

Upvotes: 0

Views: 2926

Answers (1)

Venu
Venu

Reputation: 7279

Json encode only works with UTF-8 encoded data. check whether your input data is utf8 or not

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

These are the possible errors

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

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

Upvotes: 4

Related Questions