访问JSON元素时返回“object Object”或undefined

我已经解析了json字符串,我正在尝试访问这些元素。 我无法访问link1,link2,link3

{ "click_title":"GO", "links": { "commonlink":"http:\/\/bookings.com", "alllinks": [ [ { "link1":"http:\/\/xyz1.com\/get\/a", "link2":"http:\/\/www.anotherwebsite1.com\/c\/t", "link3":"http:\/\/www.newsite1.com\/v\/h" }, { "link1":"http:\/\/xyz2.com\/get\/a", "link2":"http:\/\/www.anotherwebsite2.com\/c\/t", "link3":"http:\/\/www.newsite2.com\/v\/h" } ], [ { "link1":"http:\/\/xyz3.com\/get\/a", "link2":"http:\/\/www.anotherwebsite3.com\/c\/t", "link3":"http:\/\/www.newsite3.com\/v\/h" } ] ] } } 

var data = $ .parseJSON(dbData);

我可以使用这个访问click_title和commonlink: –

 data.click_title data.links['commonlink'] 

但无法访问link1,link2,link3。 我试过了

 data.links['alllinks'] which returns [object Object]. 

如果我试试

 data.links['alllinks']['link1'] i get undefined 

结构很奇怪。 alllinks是一个对象数组的数组(注意[和第二个["alllinks": ,每个都启动一个数组)。 要获得 一个数组中的 一个 link1 ,您需要:

 var link = data.links.alllinks[0][0].link1; 

如果您一致地格式化JSON,则更容易看到。 在这里我使用了http://jsonlint.com来清理它,然后我添加了一些注释(不是注释在JSON中有效):

 { "click_title": "GO", "links": { "commonlink": "http://bookings.com", "alllinks": [ // <== Starts the outer array [ // <== Starts an inner array { // <== Starts the object that's the first entry "link1": "http://xyz1.com/get/a", "link2": "http://www.anotherwebsite1.com/c/t", "link3": "http://www.newsite1.com/v/h" }, { // <== Starts the object that's the second entry "link1": "http://xyz2.com/get/a", "link2": "http://www.anotherwebsite2.com/c/t", "link3": "http://www.newsite2.com/v/h" } ], // <== Ends the first inner array [ // <== Starts the second inner array { "link1": "http://xyz3.com/get/a", "link2": "http://www.anotherwebsite3.com/c/t", "link3": "http://www.newsite3.com/v/h" } ] // <== Ends the second inner array ] // <== Ends the outer array } } 

所以总共有三个link1 :一个位于[0][0]的对象上,一个位于[0][1]处的对象上,另一个位于[1][0]处的对象上。