3

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\"}";

1 Answer 1

6

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

4
  • 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. Mar 19, 2010 at 5:21
  • 2
    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.
    – YOU
    Mar 19, 2010 at 5:54
  • I'm using dreamweaver and it has that option but it also has Unicode Normalization Forms option. I'm going to try notepad++
    – Nek
    Mar 19, 2010 at 5:57
  • 1
    wow! Notepad++ is the best! UTF-8 without BOM, clicked and working. Thanks S.Mark
    – Nek
    Mar 19, 2010 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.