无效的 JSON:不可见的字符?
我正在使用 JSON 在 PHP 和 Javascript/Jquery 之间切换...并且由于某种原因,我从 MSSQL 表中提取的数据间歇性地使我的 JSON 无效。我看不出这应该无效的任何原因......并且同一组数据对于其他条目工作正常。我尝试过不同的字符编码...我尝试过使用 htmlentities() 和 htmlspecialchars() 并且某些条目仍然使 JSON 无效。
这是无效 JSON 的示例
{"TASK": "test","TYPE": "Other Issue","SUBTYPE": "","CATEGORY": "","REQUEST": "Mat Gilbert","OPENDATE": "Dec 8 2010 5:12PM","PRIORITY": "2 - Low","DUEDATE": "","DESCRIPT": "12/8/2010 12:12 PM Eastern Standard Time - scldom\mgilbert test\n","STATUS": "","RESPONS": ""}
当我使用 jsonlint... 它告诉我以下内容:
语法错误,第 10 行出现意外的 TINVALID
第 10 行是“DESCRIPT”字段。
我完全不明白为什么这是无效的 JSON。请帮忙。
I am using JSON to go between PHP and Javascript/Jquery... and for some reason the data I am pulling out of a MSSQL table is intermittenly making my JSON invalid. I cannot see any reason why this should be invalid... and the same set of data works fine for OTHER entries. I've tried different char encodings... I have tried using htmlentities() and htmlspecialchars() and certian entries still render the JSON invalid.
Here is an example of invalid JSON
{"TASK": "test","TYPE": "Other Issue","SUBTYPE": "","CATEGORY": "","REQUEST": "Mat Gilbert","OPENDATE": "Dec 8 2010 5:12PM","PRIORITY": "2 - Low","DUEDATE": "","DESCRIPT": "12/8/2010 12:12 PM Eastern Standard Time - scldom\mgilbert test\n","STATUS": "","RESPONS": ""}
When I use jsonlint... it tells me the following:
syntax error, unexpected TINVALID at line 10
Line 10 is the "DESCRIPT" field.
I'm totally lost as to why this is invalid JSON. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须像
scldom\\mgilbert
一样转义这个斜杠scldom\mgilbert
。为了避免将来生成正确的 JSON 的麻烦,您可以使用 PHP 的
json_encode()
函数。You have to escape this slash
scldom\mgilbert
like soscldom\\mgilbert
.To save yourself future trouble generating proper JSON, you can use PHP's
json_encode()
function.此:
\m
无效。将其更改为\\m
This:
\m
is invalid. Change it to\\m