【发布时间】:2020-02-03 07:37:33
【问题描述】:
从SDK收到JSON Object后,console.log(JSON.stringify(obj))中的字段数量较少,
但是有些字段在console.log(JSON.stringify(obj)) 中但不在console.log(obj) 中。
【问题讨论】:
标签: javascript json stringify
从SDK收到JSON Object后,console.log(JSON.stringify(obj))中的字段数量较少,
但是有些字段在console.log(JSON.stringify(obj)) 中但不在console.log(obj) 中。
【问题讨论】:
标签: javascript json stringify
JSON.stringify() 方法将 JavaScript 对象或值转换为 JSON 字符串,如果指定了替换函数,则可选地替换值,或者如果指定了替换器数组,则可选地仅包括指定的属性。
JSON.stringify(obj, null, 2)
你可以让它打印得更好。最后一个数字决定缩进中空格的数量:
console.log(obj)
使用控制台 API,您可以将任何对象打印到控制台。这适用于任何浏览器。
【讨论】:
查看这段代码,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 对象,就像在 javascript 序列化数组中一样
2) console.log(JSON.stringify(obj)) -> 表示你将把那个 json Javascript 对象转换为字符串,然后你将使用这种方式获取数据for(var i in (JSON.stringify(obj)) { }你可以轻松获取该json中的数据
或者如果你想在javascript对象中再次转换你可以使用JSON.parse(JSON.stringify(obj))然后数据变成一个JavaScript对象。
例子:
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"}
我希望我能澄清你的问题!
【讨论】: