2

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 ( )

4
  • Can u show the code, please? :) Commented May 30, 2011 at 18:37
  • Please edit your question with this info. And use the {} (code formatting) button to format the code. CHeck the preview for how it looks Commented May 30, 2011 at 18:42
  • 1
    If a problem is solved, do not put [SOLVED] in the title. This isn't a forum or something. Just mark the most helpful answer accepted, or if there are none, post your own answer and mark it accepted when the time allows it. Questions with accepted answers (i.e. answered questions, solved problems) appear in the listing with darkgreen background and a yellow count count on the answers box. Commented May 30, 2011 at 21:06
  • ok, I'll add the answer when the site allow me. Commented May 30, 2011 at 21:29

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

I'm using php 5.3.3 now in ubuntu server
I installed PEAR JSON, how can I include it on my project?
I installed it but when trying to use it I get Class 'Services_JSON' not found
You need to setup include path and require the file: require_once 'Services/JSON.php';
0

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

1 Comment

json_last_error gives me 4, that is equal to JSON_ERROR_SYNTAX :(
0

From php.net : NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
So either you've set e recursion limit lower than the data depth (which may not seem to be the case, if you say that it does work on another page), or the json cannot be decoded.
If it works on one page and it doesn't in another, check the file encoding.
The first page may be encoded to utf-8 (the encoding of your json) but the second one might be something else (like ascii). check the bom also.Youw may need to encode the page to utf-8 without bom.

5 Comments

ok, see edit. but it still might be a different kind of utf-8
How I can disable the BOM, I use vim as my IDE
@Carlos: i'm not familiar to vim, but notepad++ has this feature and it works great with all kinds of encodings.If you do not have access to notepad++, check if any other ide's that are available to you have this options, if not, i'm sure that a simple google search will do the trick
@gion_13 I followed the instructions here gala4th.blogspot.com/2011/01/… to check if my files had BOM enabled, but they don't have BOM in the header, so now I'm really clueless...
sry, I might just be wrong, but try to set them both with bom then :|. I'm out of ideas. Maybe you should use a class or package to handle your json if the pure php fails :|
0

If it's a template you're dealing it, have a look at:

$sf_data->getRaw();

http://www.geeganage.com/symfony-json-made-easy/

1 Comment

Thank you, but I'm generating an array and then encoding it to json, then saving it on the database. I'm not using request parameters to generate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.