JSON.stringify和JSON.parse之间的区别 [英] Difference between JSON.stringify and JSON.parse
问题描述
我对何时使用这两种解析方法感到困惑。
I have been confused over when to use these two parsing methods.
在我回显我的json_encoded数据并通过ajax将其检索回来后,我常常感到困惑当我应该使用 JSON.stringify 和 JSON.parse 。
After I echo my json_encoded data and retrieve it back via ajax, I often run into confusion about when I should use JSON.stringify and JSON.parse.
我得到 [解析时在我的 console.log 中的对象,对象]
和字符串化时的JavaScript对象。
I get [object,object]
in my console.log when parsed and a JavaScript object when stringified.
$.ajax({
url: "demo_test.txt",
success: function(data) {
console.log(JSON.stringify(data))
/* OR */
console.log(JSON.parse(data))
//this is what I am unsure about?
}
});
推荐答案
JSON.stringify
将JavaScript对象转换为JSON文本并将该JSON文本存储在字符串中。
JSON.stringify
turns a JavaScript object into JSON text and stores that JSON text in a string.
JSON.parse
将一串JSON文本转换为JavaScript对象。
JSON.parse
turns a string of JSON text into a JavaScript object.
这篇关于JSON.stringify和JSON.parse之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!