tags:

views:

126

answers:

1

Hi, I'm trying to return json content read from MySQL server. This is supposed to be easy but, there is a 'weird' character that keeps appearing at start of the content.

I have two pages for returning content:

  1. kcb433.sytes.net/as/test.php?json=true&limit=6&input=d

    this test.php is from a script written by Timothy Groves, which converts an array to json output

  2. http://kcb433.sytes.net/k.php?k=4

    this one is supposed to do the same

I tried to validate it here jsonformatter.curiousconcept.com but just page 1 gets validated, page 2 says that it does not contain JSON data.

If accessed directly both pages has no problems. Then what is the difference, why both don't get validated?

Then I found this page jsonformat.com and tried the same thing. Page 1 was ok and page 2 wasn't but, surprisingly the data could be read. At a glance,

{"a":"b"}

may look good but there is a character in front.

According to a hex editor online, this is the value of the string above (instead of 9 values, there are 10):

-- 7B 22 61 22 3A 22 62  22 7D

The code to echo json in page 2 is:

header("Content-Type: application/json");
echo "{\"a\":\"b\"}";
+3  A: 

Your k.php file has BOM signature at the start, save k.php again with UTF8 without BOM.

S.Mark
I was in the process of typing this, just too slow! Would like to add: the BOM is easily visible with Fiddler, which has a built-in hex viewer.
Chris Nielsen
I did it but don't seem to work.
Nek
Which editor you using to edit php file? I noticed that Notepad does not have option for `UTF-8 without BOM`. In Notepad++, you could easily choose `UTF-8 without BOM` encoding easily.
S.Mark
I'm using dreamweaver and it has that option but it also has Unicode Normalization Forms option. I'm going to try notepad++
Nek
wow! Notepad++ is the best! UTF-8 without BOM, clicked and working. Thanks S.Mark
Nek