开发者

JSON array with properties

I have the following js array/object

var x = [1,2,3,4]; 
x.name = "myArray"; 

I am using json2.js and I am trying to serialize x in a string. and all I get is the array: [1,2,3,4]

is that correct ? since I can add any property 开发者_StackOverflow社区to an array why doesn't json2 handle that ? what am I missing ?


  1. First of all json2.js ignores properties in the array.
  2. if it had not to ignore them then it would be impossible to have an array in json format which should be easy to eval.

Let's imagine we come out with something like:

[1,2,3,4]{name:'test',anotherProperty:'someValue'} 

if the above was valid javascript to create an array and stick two properties then it would be great and we could json-it. It would be equivalent to do this:

array = [1,2,3,4]
array.name = 'test';
array.anotherProperty = 'someValue'; 

But it is not and that's the reason why we can not persist into json.


What were you expecting to see? :) I'm not sure what you're trying to do.

x is an array and all you did when you did x.name, you simply set up a property called name for the array object. The name property itself is not added into the array. You can see this in firebug:

JSON array with properties

The array object has a property called name, but the value of that property is not added to the array itself.

What you want to do is something like this:

var x = {
   array: [1, 2, 3, 4],
   name: "myArray"
};

Now you have an object called x that has two properties. The array property refers to an array, and the name property refers to the name. x.array will give you the array, and x.name will give you the name.

EDIT

This is in response to your comments. While it is true that arrays can be enriched with properties, think about what it means when you have to serialize that object. What exactly do you want to see when you serialize x? If x is already the array [1, 2, 3, 4], how would you represent it in JSON? You could represent it as:

{ 0: 1,
  1: 2,
  2: 3,
  3: 4,
  name: "myArray"
};

But what you now have is no longer an array. Also, the array object itself has a bunch of native properties. How should a JSON serializer handle those?

I think you're mixing up an array as an object and an array as a literal. An array as a literal is just [1, 2, 3, 4]. Now internally, the array as an object presumably has some property that points to the actual value of the array. It also has other properties (like length) and methods that act upon it (like slice).

The JSON serializer is concerned with the value of the object. If x is an array, then the thing that must be serialized is the value of x, which is [1, 2, 3, 4]. The properties of x are entirely different.

If you want to serialize these additional properties, then you would have to write your own serializer that will iterate over the properties of the array and represent everything in a map/associative array. This is the only way; the caveat, as I mentioned before, is that you no longer have an actual array.


The problem is json2 still believes it's an array since x instanceof Array returns true. To fix it, you could create the "array" in a roundabout way:

var x = {
    0: 1,
    1: 2,
    2: 3,
    3: 4,
    length: 4,
    name: 'myArray'
};

The problem with this solution is that none of the array methods will be available on the object. So if you ever need to use pop, push, etc, you'll have to convert it to a real array with Array.prototype.slice.call(x); or if you're byte hungry [].slice.call(x);.


Not sure why you would do it, but a for in loop would get everything

var x = [1,2,3];
x.name = "wtf";
for(var y in x){
  console.log(y + ":\t" + x[y]);
}

Produces:

0: 1
1: 2
2: 3
name: wtf

but json2 code will not see it this way, why don't you convert it over to an object?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜