最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to convert the list of array to list of json object - Stack Overflow

programmeradmin6浏览0评论

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 /

 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

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/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

Share edited Jul 30, 2012 at 14:52 Muthukumar M asked Jul 30, 2012 at 14:47 Muthukumar MMuthukumar M 1,12610 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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.

JSFIDDLE

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

try it like this:

var objectArray = [];
objectArray[0] = {};
objectArray[0]['title'] = 'name1';
objectArray[0]['start'] = new Date();
发布评论

评论列表(0)

  1. 暂无评论