How to escape special characters in building a JSON string?

后端 未结 11 854
北恋
北恋 2020-11-22 04:18

Here is my string

{
    \'user\': {
        \'name\': \'abc\',
        \'fx\': {
            \'message\': {
                \'color\': \'red\'
            }         


        
11条回答
  •  花落未央
    2020-11-22 04:40

    The answer the direct question:
    To be safe, replace the required character with \u+4-digit-hex-value

    Example: If you want to escape the apostrophe ' replace with \u0027
    D'Amico becomes D\u0027Amico

    NICE REFERENCE: http://es5.github.io/x7.html#x7.8.4

    https://mathiasbynens.be/notes/javascript-escapes

提交回复
热议问题