【问题标题】:JSON Validation - Invalid character foundJSON 验证 - 发现无效字符
【发布时间】: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.&nbsp; The only certifications recognized are those of the American Heart Association.&nbsp; These courses do NOT need to be taken on this campus.&nbsp; However, they are offered on-line and through the University Ticket Office (http:/byui.universitytickets.com).&nbsp; 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>&nbsp;under &quot;Workplace Training.&quot;&nbsp; Skills tests will need to be arranged after completing on-line portion of the course.&nbsp; 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"
        }
    ]
}

在“coursenotes”值中发现无效字符:

【问题讨论】:

  • 这个帖子应该对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


【解决方案1】:

如果我们将您的 json 复制粘贴到像 this one 这样的隐藏字符淋浴中,我们可以将奇怪的字符识别为:

Substitute character

(U+001A)


在计算机数据中,替代字符 (␚) 是一种控制字符,用于填充传输的数据,以便以固定大小的块发送数据,或代替被识别为无效的字符,在给定设备上出现错误或无法表示



在解析为 JSON 之前考虑将其替换为“普通”字符


一些相关的帖子:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多