【发布时间】:2022-01-06 12:15:45
【问题描述】:
当我点击 API 时,它的响应有一个特殊/奇怪的字符,导致 JSON 响应验证失败。即使当我尝试转义字符 JSON 验证器时也会抛出错误 Escaped unescaped character. 现在我正在尝试确定问题是来自后端还是邮递员问题。有没有人遇到过 Postman 以无法转义的无效字符返回 JSON 输出的问题?
{
"page": 0,
"totalRecords": 1,
"totalPages": 1,
"offset": 0,
"status": "success",
"entityList": [
{
"subjectcode": "HS 222",
"coursenumber": "HS 222",
"name": "Advanced First Aid",
"departments": "HRHP",
"description": "Advanced First Aid",
"status": "A",
"attributes": "",
"lastoffered": "",
"schedcode": "",
"sectionstatus": "",
"mincredithrs": "3.0",
"maxcredithrs": "3.0",
"coursenotes":"First Aid is the immediate care given to a person who has been injured or who suddenly becomes ill. It includes self-help and home care if more advanced medical assistance is not needed or is delayed. In Advanced First Aid, the student is awarded one credit for having displayed competence in both CPR and First Aid. The only certifications recognized are those of the American Heart Association. These courses do NOT need to be taken on this campus. However, they are offered on-line and through the University Ticket Office (http:/byui.universitytickets.com). For the on-line course, go to <a href=\"http://www.onlineaha.org/index.cfm?fuseaction=main.courseCatalog\">http://www.onlineaha.org/index.cfm?fuseaction=main.courseCatalog</a> and click on <a name=\"90-1401\">Heartsaver<sup></sup> First Aid CPR AED Online Part 1</a> under "Workplace Training." Skills tests will need to be arranged after completing on-line portion of the course. To successfully complete the course, each qualifying student will show his/her curent American Heart Association's First Aid and CPR Certification cards.",
"id": "HS 222 UG12"
}
]
}
【问题讨论】:
-
这个帖子应该对stackoverflow.com/questions/6234773/…有帮助
-
将 SUB 字符替换为
\u001a。 (请记住,\必须存在于 JSON 字符串末尾,因此如果您使用字符串文字进行测试,请使用另一个反斜杠转义反斜杠!) -
...但我认为这只是一个症状。如果我用谷歌搜索你的文字,我可以看到它应该是
®(注册商标)。我猜在数据库和 JSON 验证器之间的某个点(甚至可能在验证器内部!)出现字符编码问题,®不能用给定的编码表示,导致它被替换为 SUB字符(替代字符) - 导致 JSON 字符串出现问题,因为 JSON 不能包含大多数控制字符,包括 SUB 字符。 -
关于“无法转义” - 请注意,JSON 的“转义字符”并不意味着预先添加
\并称其为一天。这恰好适用于"和\本身,但其他一切都需要不同的表示,例如变成\n的换行符。任何字符,包括那些没有自己的标准转义序列(如\n)的字符,都可以使用\uXXXX进行转义,XXXX是 Unicode 代码点,所以这个字符(即字符 U+001A)可以使用\u001A进行转义。 -
我不这么认为,但很容易检查 - 使用不同的客户端或查看 Fiddler 中的原始 HTTP 请求和响应。但在这方面,也许你有一个受限的
Accept-Charset标头?你不应该有,服务器不应该再关注它们了,但谁知道......
标签: json