Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 to an array why doesn't json2 handle that ? what am I missing ?

like image 893
chacko Avatar asked Apr 08 '11 16:04

chacko


1 Answers

  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.

like image 168
chacko Avatar answered Oct 04 '22 11:10

chacko