这是我的绳子

{
    'user': {
        'name': 'abc',
        'fx': {
            'message': {
                'color': 'red'
            },
            'user': {
                'color': 'blue'
            }
        }
    },
    'timestamp': '2013-10-04T08: 10: 41+0100',
    'message': 'I'mABC..',
    'nanotime': '19993363098581330'
}    

Here the message contains single quotation mark, which is same as the quotation used in JSON. What I do is fill up a string from user inputs such as message. So, I need to escape those kind of special scenarios which breaks the code. But other than string replace, is there any way to make them escape but still allow HTML to process them back to the correct message?

推荐答案

根据specs,JSON字符串必须是双引号,所以不需要转义'

请参阅JSON中使用的特殊字符列表:

\b  Backspace (ascii code 08)
\f  Form feed (ascii code 0C)
\n  New line
\r  Carriage return
\t  Tab
\"  Double quote
\\  Backslash character


然而,即使它与规范完全相反,作者也可以使用\'.

这是bad,因为:

  • It IS contrary to the specs
  • 它不再是JSON有效字符串

But it works, as you want it or not.

For new readers, always use a double quotes for your json strings.

Json相关问答推荐

如何在JQ过滤器文件中使用多行?

时间序列的Vega Lite分组条形图

如何创建生成两个不同对象的JSON数组的SQL查询?

在Reaction中从JSON文件中筛选数组

将 REST API - json 输出转换为表 Power BI

当并非所有子对象都有 Select 器字段时 Select

使用 jq 如何更改键的值?

如何强制仅有一个元素的数组在JSON中生成方括号

具有 (RegEx) 模式的 json-schema 中的枚举

使用 map_values Select 包含空格的字符串未按预期工作

JOLT 在 struct 体中间添加一个 JSON 字段

下一个 Js 部署在 getStaticPath 函数上失败:在 JSON.parse 的位置 0 的 JSON 中出现意外的令牌 < 但在本地运行

如果 jq 数组中的字符串是对象或字符串,则获取值

JSON 模式验证

如何使用 Serde 使用顶级数组反序列化 JSON?

如何在 Django JSONField 数据上聚合(最小/最大等)?

JSON Schema:验证数字或空值

在 React 中访问子级的父级状态

带有 Jackson 的不可变 Lombok 注释类

Newtonsoft 对象 → 获取 JSON 字符串