Tackling json encode problem in php
Have you ever came accross problems writing web services where in you cannot encode your associate array into a json_encode string ? then you are at right place.
To solve this problem It really took lot of time to find the exact solution .. ☹
with me I was not getting any o/p and if i try to encode it was giving me the worst o/p possible . So after beating my head alot (which I often do ) I tried finding many things and I got thie json_last_error_msg();I call this function a life saver function because if you have any error in your json encode this function will throw the exact error and then you can find the hack for that perticular problem .
hisSo In my case I was retrieving all viered data stored using wordpress through m web serive and while doing the json encode my array was not geting encoded to the json string
Fix for this problems
Include mysqli_set_charset($connection,”utf8") before executing your database query , json encode your array using this parameters
json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE)
this will always give you the correct and printable o/p. header(‘Content-Type: application/json’); and you are done with fixing of your problem related to all json encode . hurray …….