How to escape special characters in building a JSON string?

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

Here is my string

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


        
11条回答
  •  忘了有多久
    2020-11-22 04:34

    I think we all agree single quoted jsons aren't real jsons. Be that as it may, we still need to address the question of escaping " within a double quoted json string, in the absence of libraries to do so for us.

    Replacing each " with a \" is NOT ENOUGH: User may enter the input: \ and parsing, again, fails (think why).

    Instead, first replace each \ with \ (double backslash). Only then, replace each " with \" (backslash followed by ").

提交回复
热议问题