Currently I’m working on calling a JSON feed (from an URL) from within an Android application. Calling this URL is no problem, but I’m having a really annoying issue with my JSON output. Yesterday my JSON was working fine, but when I made a call to it today I’m getting an error that my string cannot be converted to a JSON object.
I printed the result to the console and the JSON string looked just fine.
{"message":{"success":false,"error":22}}
However, using the substring() method to obtain only the first character of the string, the output was:
·
I’m assuming this is an encoding issue, but I’m giving the following headers to my JSON feed:
content-type: application/json; charset=utf-8
I’m using the Yii-framework method CJSON::encode() to encode the data to JSON. It has always worked on me, until this morning. I set all my encoding to utf-8, but with no avail.
I also called to the JSON-feed from my browser, copied the JSON and used an online JSON validator, which gave me an ‘Invalid JSON’ result on the first line which is the ‘{‘ bracket. I removed the bracket and manually added it, validated the JSON again and the validator gave a ‘Valid JSON’ result.
Are you using PHP end tag
?>on all your custom PHP files?if so, remove the end tag. It is optional in PHP. And make sure that PHP open tag
<?phpalways start at top of the file without any spaces in your classes/models/controllers.They create this kind of special characters, because after the end tag whatever you put will be a output to the client. NOT all PHP editors show special characters they may just display some blank space.
one way to debug is … use
ob_start() ob_end_clean()simply add
ob_start()on top of the front script … i.e. add it in index.phpand before your
CJSON::encode()clean the buffer usingob_end_clean()