utf 8

I have a symfony app that uses the json_encode and json_decode to keep a record of some prices. The problem is that json_decode works OK in one file (I can decode the string stored in my PSQL database), but when I call it from other file json_decode returns null, I've check the file encodings (all are utf-8) the tables and database encoding(utf-8 too). So I don't know where the problem can be, tried utf8_encode() too...

Any help will be appreciated. Thanks.

Here's the valid encoded json (It was an array encoded by php json_encode)

{"1":{"1":{"fechaInicio":"30-05-2011","precios":{"1":{"precio":"20000","abreviatura":"CLP"}},"fechaRetiro":"31-05-2011"}},"2":{"2":{"fechaInicio":"30-05-2011","precios":{"1":{"precio":"20000","abreviatura":"CLP"}},"fechaRetiro":"31-05-2011"}}}

The array:

$preciosOfertor = Array ( [unidades] => Array ( [1] => Array ( [1] => Array ( [fechaInicio] => 30-05-2011 [precios] => Array ( [1] => Array ( [precio] => 20000 [abreviatura] => CLP ) ) [fechaRetiro] => 31-05-2011 ) ) [2] => Array ( [2] => Array ( [fechaInicio] => 30-05-2011 [precios] => Array ( [1] => Array ( [precio] => 20000 [abreviatura] => CLP ) ) [fechaRetiro] => 31-05-2011 ) ) ) ) 

To encode it I use:

$preciosOfertor = json_encode($preciosOfertor); 

Then I call

$precios = json_decode($databaseObject->getPreciosOfertor(),true); 

When i use json_decode in the file that encodes the array it works, but then when I use it in other file of the project I just get NULL with var_dump().

Installed Services_JSON as suggested, but now I'm getting an empty array

The encoded json with Services_JSON is this one:

{"unidades":{"1":{"1":{"fechaInicio":"30-05-2011","precios":{"1":{"precio":"20000","abreviatura":"CLP"}},"fechaRetiro":"31-05-2011"}}}}

But when I call $json->decode() I get Array ( )


Ok people, first thank you all for the help.

I got the solution and It was all thanks to Zend Json library.

Symfony uses escaping strategies to prevent XSS attacks, SQL Injection attacks, etc. So what happened here in my case, when I called json_encode and json_decode it was inside the object that Doctrine generates to represent my object (In this case a reservation), so because it was a local call to the row data (valoresOfertor), the data from the database was not escaped thus the methods worked fine.

But then, when I tried to encode and decode the values of the row outside the reservation class, Symfony used it's escaping strategy so

"

became

&quot

So, trying different JSON libraries, I used Zend one, and saw the exception that displayed (Syntax Error:

    at Zend_Json::decode('{"unidades":{"1":{"1":{"fechaInicio":"30-05-2011","precios":{"1":{"precio":"20000","abreviatura":"CLP"}},"fechaRetiro":"31-05-2011"}},"2":{"2":{"fechaInicio":"30-05-2011","precios":{"1":{"precio":"20000","abreviatura":"CLP"}},"fechaRetiro":"31-05-2011"}}}}')
in SF_ROOT_DIR/apps/saas/modules/editreserva/templates/habitacionesSuccess.php line 20 ...

So then i Added the following line:

htmlspecialchars_decode($jsonVariable);

And it worked.

I hope it helps someone if he experiments the same with symfony and json.


As far as I know, there were one or more bugs in json_encode() and json_encode() in previous versions of PHP. You could try to update PHP to the latest version or you could use an external library to encode and decode JSON. There are some, but I think PEAR JSON is the best.


it might be the UTF-8 BOM present. Try using UTF without BOM encoding. Also echo the json_last_error() to see what's the problem.

EDIT:

It IS a valid JSON

链接地址: http://www.djcxy.com/p/57670.html

上一篇: 对PHP多维数组格式感到困惑

下一篇: utf 8