客户端和服务器端的新行如何绑定?

时间:2021-12-08 15:39:17

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

我有如下对象{g:“a\nb”},我用JSON.stringify将其stringify。

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

在客户端'{"g":"a\nb"}被返回,但是在服务器端一个转义字符被添加:'{"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?

为什么会这样?

1 个解决方案

#1


3  

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.

这是一个渲染问题,结果是相等的。它只是在Chrome的控制台以不同的方式显示它们的清晰度。它以更可读的格式显示。

The Node console doesn't do any such magic.

节点控制台不会做任何这样的魔术。

You can observe this in the chrome console:

你可以在chrome控制台观察到:

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

#1


3  

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.

这是一个渲染问题,结果是相等的。它只是在Chrome的控制台以不同的方式显示它们的清晰度。它以更可读的格式显示。

The Node console doesn't do any such magic.

节点控制台不会做任何这样的魔术。

You can observe this in the chrome console:

你可以在chrome控制台观察到:

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