简体   繁体   English

为什么不在javascript中转换对象和从JSON转换对象?

[英]Why doesn't converting an object in javascript to and from JSON return the object?

I'm having an issue comparing objects encoded and decoded to and from JSON 我在比较JSON编码和解码的对象时遇到了问题

//Test data
var test_obj = {
    test:'value',
    t:3,
    x:[0,5,3]
};
var t = JSON.stringify(test_obj);
var t_prime = JSON.parse(t);

You'd think that test_obj === t_prime would return true, or perhaps test_obj == t_prime would return true, but this is not the case. 你认为test_obj === t_prime会返回true,或者test_obj == t_prime会返回true,但事实并非如此。

Why is this, and how can I verify that I get the same object to and from a javascript object to JSON? 为什么这样,我如何验证我从javascript对象到JSON获取相同的对象?

When you compare objects in JS, you are checking to see if they are the same object and not identical objects . 比较JS中的对象时,您要检查它们是否是同一个对象而不是相同的对象

Converting to JSON turns an object into a string. 转换为JSON会将对象转换为字符串。 Converting from JSON creates a new object based on the JSON data. 从JSON转换基于JSON数据创建对象。

If you want to check if two objects are identical, then see How do you determine equality for two JavaScript objects? 如果要检查两个对象是否相同,请参阅如何确定两个JavaScript对象的相等性? .

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

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