简体   繁体   中英

How are the new line stringified on the client side vs server side?

I have the following object {g: "a\\nb"} that I stringify using JSON.stringify .

On the client side '{"g":"a\\nb"}' is returned but on the server side an escaping character is added: '{"g":"a\\\\nb"}' .

Chrome console

Google Chrome console view

Node console

> a = {g: "a\nb"}
{ g: 'a\nb' }
> JSON.stringify(a)
'{"g":"a\\nb"}'

Why is this happening?

This is a rendering issue, the results are equal. It just shows them differently in the Chrome console for clarity. It shows it in a more readable format.

The Node console doesn't do any such magic.

You can observe this in the chrome console:

JSON.stringify({g: "a\nb"}) === '{"g":"a\\nb"}'; // true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM