Reputation: 1126
i like to convert array to json object like this
var obj = [{item:'name1',start:new date()}, {item:'name2',start:new date()},{item:'name3',start:new date()}]
i am using single dimension array means is working fine. check this link http://jsfiddle.net/H4ezf/1/
var objectArray= {};
objectArray['title']='All Day Event';
objectArray['start']=new Date(y, m, 1);
console.log(JSON.stringify(objectArray));
output as : {"title":"All Day Event","start":"2012-06-30T18:30:00.000Z"}
but i try to convert list of array to list json object using json stringify like this
var objectArray= {};
objectArray[0]['title']='name1';
objectArray[0]['start']=new Date();
objectArray[1]['title']='name2';
objectArray[1]['start']=new Date();
console.log(JSON.stringify(objectArray));
it not working. what i am wrong here. Please any one can help me to solve this problem
Upvotes: 2
Views: 3146
Reputation: 6479
var objectArray= [];
objectArray[0] = {}
objectArray[0]['title']='name1';
objectArray[0]['start']=new Date();
objectArray[1] = {}
objectArray[1]['title']='name2';
objectArray[1]['start']=new Date();
console.log(JSON.stringify(objectArray));
Upvotes: 2
Reputation: 4180
try it like this:
var objectArray = [];
objectArray[0] = {};
objectArray[0]['title'] = 'name1';
objectArray[0]['start'] = new Date();
Upvotes: 1
Reputation:
You cannot do this:
var objectArray= {};
objectArray[0]['title']='name1';
as objectArray[0]
does not exist yet. There is no array at that index and therefor you cannot add a string at an index. You have do declare the array first. The rest of your code works just fine.
Upvotes: 6