我正在使用kendo UI,但我认为这是一个普遍的问题.
基本上我正在记录一个对象,结果看起来像这样,
然后我JSON.stringify(obj),我得到这样的输出
{"ProductID":5,"ProductName":"Cream","UnitPrice":12,"Discontinued":false,"UnitsInStock":15,"Priority":5}
Run Code Online (Sandbox Code Playgroud)
问题,有人可以说明为什么参数"脏",'uid'没有得到字符串化?如果您能够实际发布一些创建此类对象的示例,那将会很棒.
仅供参考:我的实际对象就像stringify的输出一样,传递给了剑道网格,我从其中一个剑道网格方法中获取了脏对象(基本上给出了在网格中编辑过的行集)
有什么想法与object.prototype有关.也许父属性没有得到字符串化......
JSON.stringify仅包括对象的自己,枚举的属性,其名称是字符串.因此有三种方法可以省略属性:如果它们是继承的,如果它们是不可枚举的(例如Object.defineProperty默认创建的那些),或者它们的名称不是字符串(ES2015具有属性的能力)有Symbol名字而不是字符串名称).
这演示了其中的两个,"自己的"和"可枚举的"方面:{"answer":42}例如,它记录因为obj只有一个自己的,可枚举的属性,answer.prop是继承的,foo是不可枚举的:
// An object to use as a prototype
var proto = {
prop: 1
};
// Create an object using that as its prototype
var obj = Object.create(proto);
// Define a non-enumerable property
Object.defineProperty(obj, "foo", {
value: 27
});
// Define an own, enumerable property
obj.answer = 42;
// Show the JSON for the object
snippet.log(JSON.stringify(obj));
Run Code Online (Sandbox Code Playgroud)
这在以下规范中JSON.stringify:
JSON.stringify 调用抽象规范操作 SerializeJSONProperty
SerializeJSONProperty,对于对象,最终调用 SerializeJSONObject
SerializeJSONObject调用EnumerableOwnProperties以获取要序列化的属性列表
实例:
// An object to use as a prototype
var proto = {
prop: 1
};
// Create an object using that as its prototype
var obj = Object.create(proto);
// Define a non-enumerable property
Object.defineProperty(obj, "foo", {
value: 27
});
// Define an own, enumerable property
obj.answer = 42;
// Show the JSON for the object
snippet.log(JSON.stringify(obj));Run Code Online (Sandbox Code Playgroud)
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>Run Code Online (Sandbox Code Playgroud)
为了完整Symbol起见,这个演示了 - 命名的属性也被遗漏了(Babel的REPL上的实时副本):
// An object to use as a prototype
var proto = {
prop: 1
};
// Create an object using that as its prototype
var obj = Object.create(proto);
// Define a non-enumerable property
Object.defineProperty(obj, "foo", {
value: 27
});
// Define an own, enumerable property with a string name
obj.answer = 42;
// Define an own, enumerable property with a Symbol name
var s1 = Symbol();
obj[s1] = "I'm enumerable and have a Symbol name";
// Show the JSON for the object
console.log(JSON.stringify(obj)); // {"answer":42}
// Proof that the Symbol property was enumerable comes
// from Object.assign
var obj2 = Object.assign(obj);
console.log(obj2[s1]); // I'm enumerable and have a Symbol name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
497 次 |
| 最近记录: |