[英]why JSON.stringify is adding extra “”
My application receives the following message from the server 我的应用程序从服务器收到以下消息
got error from the Observable: {result: "error", additional-info: "", http-status: "401", http-status-text: "Unauthorized"}
I parse it into a variable 我将其解析为一个变量
error:ServerResponseAPI
If I do JSON.stringify(error['additional-info'])
, I notice that the result is """"
instead of ""
如果我执行
JSON.stringify(error['additional-info'])
,我注意到结果是""""
而不是""
Why? 为什么?
""
is an empty string, which is valid JSON (any single string is valid JSON). ""
是一个空字符串,它是有效的JSON(任何单个字符串都是有效的JSON)。 If you use JSON.stringify
on an empty string, it will stringify/serialize the provided value into JSON. 如果对空字符串使用
JSON.stringify
,它将把提供的值字符串化/序列化为JSON。 In this case that's an empty string, so it will give you a string of quotes. 在这种情况下,这是一个空字符串,因此它将为您提供一串引号。 This is why you might see
'""'
, or """"
when logging the value. 这就是为什么在记录值时可能会看到
'""'
或""""
的原因。 It's a string that contains a JSON representation of an empty string (which is the two quotes). 它是一个字符串,其中包含空字符串的JSON表示形式(这是两个引号)。
JSON.stringify('a')
will give you '"a"'
, for example. JSON.stringify('a')
会给你'"a"'
。
The outer quotes is to indicate that this content is inside a string. 外部引号表示该内容在字符串内。
Your json content is an empty string represented inside a real string, it could be presented with escapes: "\\"\\""
. 您的json内容是一个用实字符串表示的空字符串,可以用转义符
"\\"\\""
。
If error['additional-info']
is null, JSON.stringify(error['additional-info'])
would return "null"
. 如果
error['additional-info']
为null,则JSON.stringify(error['additional-info'])
将返回"null"
。 The content in this case is the null
value, without quotes. 在这种情况下,内容为
null
值,不带引号。
JSON.stringify
will never return an empty string. JSON.stringify
将永远不会返回空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.