javascript JSON.stringify 忽略对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20511421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON.stringify is ignoring object properties
提问by frigon
See the jsfiddle example http://jsfiddle.net/frigon/H6ssq/
请参阅 jsfiddle 示例http://jsfiddle.net/frigon/H6ssq/
For some reason there are fields that JSON.stringify is ignoring. Is there a way to force JSON.stringify to parse them?
出于某种原因,JSON.stringify 忽略了一些字段。有没有办法强制 JSON.stringify 解析它们?
As the jsfiddle shows... this code...
正如 jsfiddle 所示......这段代码......
<script src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script>
<script>
var model = kendo.data.Model.define({id: "ID", fields: {"Name":{type: "string"}}});
var obj = new model();
obj.set("Name","Johhny Foosball");
document.write("<br />obj.dirty property exists: ");
document.write(obj.dirty);
document.write("<br/>obj.uid property exists: ");
document.write(obj.uid);
document.write("<br/>But they dont show in JSON.stringify():<br/>");
document.write(JSON.stringify(obj));
</script>
will output:
将输出:
obj.dirty property exists: true
obj.dirty 属性存在:true
obj.uid property exists: b4af4dfc-9d94-4a2d-b286-d6f4cbc991d8
obj.uid 属性存在:b4af4dfc-9d94-4a2d-b286-d6f4cbc991d8
But they dont show in JSON.stringify():
但它们没有显示在 JSON.stringify() 中:
{"ID":"","Name":"Johhny Foosball"}
{"ID":"","Name":"约翰尼足球"}
回答by Kevin Babcock
When an object has its own toJSON()
implementation, JSON.stringify()
uses the object returned from that method and stringifies that. kendo.data.Model
defines it's own toJSON()
method which only returns the properties defined on the model, which is why you aren't seeing other values (e.g. dirty
, id
, uid
) in the string result.
当对象有自己的toJSON()
实现时,JSON.stringify()
使用从该方法返回的对象并将其字符串化。kendo.data.Model
定义它自己的toJSON()
方法,该方法只返回在模型上定义的属性,这就是为什么您在字符串结果中看不到其他值(例如dirty
, id
, uid
)的原因。
Take a look at this article. Specifically: "If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation."
看看这篇文章。具体来说:“如果 stringify 方法看到一个包含 toJSON 方法的对象,它会调用该方法,并将返回的值字符串化。这允许对象确定自己的 JSON 表示。”
Here's an alternative if you musthave all properties on the object:
如果您必须在对象上拥有所有属性,这是一种替代方法:
var model = kendo.data.Model.define({
id: "ID",
fields: {
"Name": { type: "string" }
}
});
var obj = new model();
obj.set("Name","Johhny Foosball");
var newObj = $.extend({}, obj);
delete newObj.toJSON;
document.write(newObj.dirty);
document.write(JSON.stringify(newObj));
..and the updated fiddle.
..和更新的小提琴。
Basically I used jQuery.extend
to clone the object then deleted the toJSON
function. Use at your own risk! :)
基本上我用来jQuery.extend
克隆对象然后删除toJSON
函数。使用风险自负!:)
回答by Nikos Papageorgiou
Somewhat relevant, as I came here looking for a solution to a similar situation: For those looking for a way to get back their original object, before kendo added all sorts of weird hidden properties to it, do:
有点相关,当我来到这里寻找类似情况的解决方案时:对于那些正在寻找一种方法来找回原始对象的人,在剑道向其添加各种奇怪的隐藏属性之前,请执行以下操作:
cleanObject = JSON.parse(JSON.stringify(kendoDataItem));
I encountered this scenario when I needed to pass the selected item from the kendo tree to a json-formatter directive. But what do you know, kendo had allready messed up with my original object.
当我需要将所选项目从剑道树传递给 json-formatter 指令时,我遇到了这种情况。但是你知道吗,剑道已经把我原本的对象搞砸了。
<div kendo-tree-view="tree" k-data-source="treeData" k-on-change="selectedItem=dataItem">
{{dataItem.text}}
</div>
<json-formatter open="1" json="selectedItem"></json-formatter>