[英]Difference between console.log(obj) and console.log(JSON.stringify(obj))
从 SDK 收到 JSON Object 后, console.log(JSON.stringify(obj))
中的一些字段在console.log(JSON.stringify(obj))
但 notlog) 中的字段数量较少,但存在一些字段console.log(obj)
。
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
JSON.stringify(obj, null, 2)
你可以让它打印得更好。 最后一个数字确定缩进中的空格数量:
console.log(obj)
使用控制台 API 您可以将任何 object 打印到控制台。 这适用于任何浏览器。
查看这段代码,console.log 中的结果正在发生变化。
const obj = { value: 5 }; console.log(obj); setTimeout(() => { obj.value = 10; }, 100);
如果使用 console.log(JSON.stringify(obj)) 可以获得原始值
const obj = { value: 5 }; console.log(JSON.stringify(obj)); setTimeout(() => { obj.value = 10; }, 100);
当您从创建的 json 获取数据时,您可以像这样使您的问题更清楚
1) console.log(obj) -> 意味着你会得到 json Javascript object 就像在 ZDE9B9ED78D7E2E9191DCEEFFEE780ZE
( for(var i in (JSON.stringify(obj)) { }
您可以轻松地在for(var i in (JSON.stringify(obj)) { }
中获取数据
or if you want to convert again in javascript object you can use JSON.parse(JSON.stringify(obj))
then the data becomes a JavaScript object.
例子:
var obj = { name: "John", age: 30, city: "New York" };
1) Use the JavaScript function JSON.stringify() to convert it into a string.
var myJSON = JSON.stringify(obj);
2) Use the JavaScript function JSON.parse() to convert text-string into a JavaScript object:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
1) console.log(obj);
output : {name: "John", age: 30, city: "New York"}
2) console.log(myJSON );
output : {"name":"John","age":30,"city":"New York"}
2) console.log( JSON.parse(myJSON ));
output : {name: "John", age: 30, city: "New York"}
我希望我能澄清你的问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.