简体   繁体   English

JSON.stringify(对象)不正确

[英]JSON.stringify(object) incorrect

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. 如您所见, JSON.stringify(obj)的输出是错误的。 What causes this ? 是什么原因造成的?

You're first building an array ( [] ), then assigning properties to it with non-number keys ( player ). 首先要构建一个数组( [] ),然后使用非数字键( player )向其分配属性。 This is technically possible (as in not causing an error), but it's not what arrays are for. 从技术上讲这是可能的(因为不会导致错误),但这不是数组的目的。

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 )作为键。

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'}}
  ]
};

You're using named array indeces instead of object key/value pairs. 您正在使用命名数组索引而不是对象键/值对。

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));

entities , and entities["player"] and entities["player"]["0"] need to be objects , not arrays. entitiesentities["player"]entities["player"]["0"]必须是对象 ,而不是数组。

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";

I think you have some serious confusions about arrays and objects in javascript. 我认为您对javascript中的数组和对象有一些严重的困惑。 An array ( [] ) works only with positive integer indexes. 数组( [] )仅适用于正整数索引。 In your example you are doing the following: 在您的示例中,您正在执行以下操作:

obj.entities = [];
obj.entities["player"] = [];

You say that obj.entities is an array and then you use player as index. 您说obj.entities是一个数组,然后将player用作索引。 And player is not an integer. player不是整数。 So this code makes no sense. 因此,这段代码毫无意义。 On the other hand you could use objects with properties. 另一方面,您可以使用具有属性的对象。 Those properties can be strings: 这些属性可以是字符串:

obj.entities = {};
obj.entities.player = [];
obj.entities.player[0] = 'foo bar';

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: 您出错的地方:

  1. An array can have only integer indexes. 数组只能有整数索引。 So it's incorrect writing a["1"] if you intend to use a as an array . 因此, 如果您打算将a用作数组,则写a [“ 1”]是不正确的。
  2. To be correctly serialized, only an object can have named properties, like object.entities.player or object.entities["player"] . 为了正确地序列化,只有对象可以具有命名属性,例如object.entities.playerobject.entities["player"]

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

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