0

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.

Sietse
  • 180
  • 2
  • 13
  • 4
    And what happened this morning, when it stopped working? – bartlaarhoven Oct 18 '12 at 10:41
  • 1
    Have you checked the string in a hex editor to get the hex value of each character? Could be that you've got some strange whitespace character in front of your opening "{" that is not visible on the screen. – GarethL Oct 18 '12 at 10:44
  • It started to give the weird character infront of the JSON string, which is not visible from the browser. When looking at the feed from the browser, the page outputs: `There was an error parsing the JSON document. The document may not be well-formed.` – Sietse Oct 18 '12 at 10:45
  • @GarethL I think I indeed have some strage whitespace in front of my opening {, as looking at the string with the hex editor returned: `00000000: -- -- -- -- 7B 22 75 73 65 72 22 3A -- -- 7B 22 00000010: 6D 65 73 73 61 67 65 22 3A 7B 22 73 75 63 63 65 00000020: 73 73 22 3A 66 61 6C 73 65 2C 22 65 72 72 6F 72 00000030: 22 3A 32 32 7D 7D` – Sietse Oct 18 '12 at 10:54
  • `--` is not a hexcode. This is especially sad because I'd say these masked parts are the most interesting. Please remove the masking and show the real values. – hakre Oct 18 '12 at 10:57
  • What do you mean remove the masking? I literally copied the JSON feed from the browser to my hex-editor and it showed -- -- infront of 7B, which refers to the opening "{". – Sietse Oct 18 '12 at 10:59
  • 1
    The `--` parts are a placeholder for a specific character, likely in the control character range. The value of these characters are *masked*, you don't see their real value but a placeholder (mask) instead, the `--` string. Copying over from the browser and pasting into your hexeditor might create that, however, if you want to fully analyze what is going on, you need to get the real values of these characters. I'm not fluent with your IDE, however, you should log the real hex values of that json response and then copy that log output instead of the browser output. – hakre Oct 18 '12 at 11:11
  • As far as PHP is concerned with this, you might find this question useful to create a hex dump of a PHP string: [How can I get a hex dump of a string in PHP?](http://stackoverflow.com/q/1057572/367456). – hakre Oct 18 '12 at 11:12
  • Thanks hakre, using that function returned `7b226d657373616765223a7b2273756363657373223a66616c73652c226572726f72223a32327d7d` – Sietse Oct 18 '12 at 11:25

1 Answers1

3

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 <?php always 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.

<?php
    echo "Hello, World,";

?>
__//Here is a special character, but editor unable to display it.

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.php

  • and before your CJSON::encode() clean the buffer using ob_end_clean()

SuVeRa
  • 2,784
  • 2
  • 20
  • 27