javascript JSON.stringify(object) 不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8979093/
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(object) incorrect
提问by pimvdb
Sorry for my last question being so confusing, I was confused my self, but now I got a proper example:
对不起,我的最后一个问题如此令人困惑,我自己也感到困惑,但现在我有了一个正确的例子:
var obj = {};
obj.entities = [];
obj.entities["player"] = [];
obj.entities["player"]["0"] = [];
obj.entities["player"]["0"]["pos"] = "0,0";
var jsonStr = JSON.stringify(jsonObj);
// {"entities":[]}
console.log(JSON.stringify(obj));
The output of JSON.stringify(obj)
is wrong as you can see.
What causes this ?
JSON.stringify(obj)
如您所见,输出是错误的。这是什么原因造成的?
回答by pimvdb
You're first building an array ([]
), then assigning properties to it with non-number keys (player
). This is technically possible (as in not causing an error), but it's not what arrays are for.
您首先构建一个数组 ( []
),然后使用非数字键 ( player
)为其分配属性。这在技术上是可行的(因为不会导致错误),但这不是数组的用途。
You should use objects ({}
) instead. Also, ["player"]
is the same as .player
.
您应该改用对象 ( {}
)。此外,["player"]
与.player
.
var obj = {};
obj.entities = {};
obj.entities.player = []; // array here because you're filling with indices ([0])
obj.entities.player[0] = {}; // object again, because non-indices as keys (`pos`)
obj.entities.player[0].pos = "0,0";
Objects can take any property key. Arrays are a subset of objects, which should only have indices (numbers >= 0
) as keys.
对象可以采用任何属性键。数组是对象的子集,它应该只具有索引(数字>= 0
)作为键。
回答by Dor Shemer
Your life would be much easier if you'd define your objects in JSON to begin with:
如果您开始在 JSON 中定义对象,您的生活会容易得多:
var obj = {
'entities': [
{'player':{'pos': '0,0'}}
]
};
回答by Darin Dimitrov
I think you have some serious confusions about arrays and objects in javascript. An array ([]
) works only with positive integer indexes. In your example you are doing the following:
我认为您对 javascript 中的数组和对象有一些严重的困惑。数组 ( []
) 仅适用于正整数索引。在您的示例中,您正在执行以下操作:
obj.entities = [];
obj.entities["player"] = [];
You say that obj.entities
is an array and then you use player
as index. And player
is not an integer. So this code makes no sense. On the other hand you could use objects with properties. Those properties can be strings:
你说这obj.entities
是一个数组,然后你player
用作索引。并且player
不是整数。所以这段代码没有意义。另一方面,您可以使用具有属性的对象。这些属性可以是字符串:
obj.entities = {};
obj.entities.player = [];
obj.entities.player[0] = 'foo bar';
回答by frm
You are confusing objects with arrays. The following code will work.
您将对象与数组混淆了。以下代码将起作用。
var obj = {};
obj.entities = {};
obj.entities.player = [];
obj.entities.player[0] = {};
obj.entities.player[0].pos = "0,0";
The things you went wrong:
你出错的地方:
- An array can have only integer indexes. So it's incorrect writing a["1"] if you intend to use
a
as an array. - To be correctly serialized, only an object can have named properties, like
object.entities.player
orobject.entities["player"]
.
- 数组只能有整数索引。因此,如果您打算
a
用作数组,则编写 a["1"] 是不正确的。 - 要正确序列化,只有对象可以具有命名属性,例如
object.entities.player
或object.entities["player"]
。
回答by Nick Jurista
You're using named array indeces instead of object key/value pairs.
您正在使用命名数组 indeces 而不是对象键/值对。
var obj = {};
obj.entities = {};
obj.entities["player"] = {};
obj.entities["player"]["0"] = [];
obj.entities["player"]["pos"] = "0,0";
// {"entities":{"player":{"0":[],"pos":"0,0"}}}
console.log(JSON.stringify(obj));
回答by Adam Rackis
entities
, and entities["player"]
and entities["player"]["0"]
need to be objects, not arrays.
entities
, and entities["player"]
andentities["player"]["0"]
需要是objects,而不是数组。
You're adding properties to these arrays, rather than pushing values onto them. These custom properties are not getting stringified.
您正在向这些数组添加属性,而不是将值推送到它们上。这些自定义属性没有被字符串化。
The fix is simple:
修复很简单:
var obj = {};
obj.entities = {}; // <------------ this is an object now
obj.entities["player"] = {}; // <--------- also an object
obj.entities["player"]["0"] = {}; // <-------- and so on
obj.entities["player"]["0"]["pos"] = "0,0";