奇怪的字符在json内容类型的开始 [英] Weird character at start of json content type
问题描述
我正在尝试返回从MySQL服务器读取的json内容。这应该很容易,但是有一个'奇怪'的字符在内容开始时出现。
我有两个页面用于返回内容:
-
kcb433.sytes.net/as/test.php?json=true&limit=6&input=d
此test.php来自Timothy Groves编写的脚本,它将数组转换为json输出
-
http://kcb433.sytes.net/k.php?k=4
这个应该是做同样的
a href =http://jsonformatter.curiousconcept.com =nofollow noreferrer> jsonformatter.curiousconcept.com 但只有第1页获得验证,第2页说它不包含JSON数据。 p>
如果直接访问这两个页面没有问题。
然后我发现这个页面 jsonformat.com 并尝试了同样的事情。第一页是确定,第二页是不是,但令人惊讶的是,数据可以阅读。一目了然,
{a:b}
pre>
看起来不错,但前面有一个字符。
是上面的字符串的值(而不是9个值,有10个):
- 7B 22 61 22 3A 22 62 22 7D
在第2页回应json的代码是:
header(Content-Type:application / json);
echo{\a\:\b \};
解决方案
k.php
文件开头有 BOM签名,保存k.php
再次使用UTF8而不使用BOM。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:
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
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\"}";
解决方案Your
k.php
file has BOM signature at the start, savek.php
again with UTF8 without BOM.这篇关于奇怪的字符在json内容类型的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!