How do I retain backslashes in strings when using JSON.stringify?

后端 未结 3 2363
忘掉有多难
忘掉有多难 2021-02-20 16:15

So I got a string that has a backslash in it. \"kIurhgFBOzDW5il89\\/lB1ZQnmmY=\".

I tried adding an extra \'\\\', but JSON.stringify( \"kIurhgFBOzDW5i

3条回答
  •  终归单人心
    2021-02-20 16:56

    The backslash is escaping the forward slash. So JSON.stringify("\/") returns "/" since it sees an escaped forward slash, so its just a forward slash. JSON.stringify("\\/") sees a backslash being escaped, and then a forward slash next to that, so it returns "\/". You cannot preserve the "exact" string when you stringify, since parsing a json string will not escape characters, so you get back your original data, just unescaped.

提交回复
热议问题