简体   繁体   English

新行在客户端和服务器端如何分类?

[英]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 . 我有以下对象{g: "a\\nb"} ,我使用JSON.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 Chrome控制台

Google Chrome console view Google Chrome控制台视图

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. 为了清楚起见,它只是在Chrome控制台中以不同的方式显示了它们。 It shows it in a more readable format. 它以更具可读性的格式显示它。

The Node console doesn't do any such magic. Node控制台没有做任何这样的魔术。

You can observe this in the chrome console: 您可以在chrome控制台中观察到这一点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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