"对象的对象"或在访问JSON元素未定义返回 [英] "object Object" or undefined returned while accessing JSON elements
问题描述
我解析JSON字符串,我试图访问的元素。我无法访问链接1,链接2,LINK3
{
click_title:GO,
链接:
{
commonlink:HTTP:\ / \ / bookings.com
alllinks:
[
[
{
链路1:HTTP:\ / \ / xyz1.com \ /获取\ / A,
链接2:HTTP:\ / \ / www.anotherwebsite1.com \ / C \ / T,
LINK3:HTTP:\ / \ / www.newsite1.com \ / V \ /小时
},
{
链路1:HTTP:\ / \ / xyz2.com \ /获取\ / A,
链接2:HTTP:\ / \ / www.anotherwebsite2.com \ / C \ / T,
LINK3:HTTP:\ / \ / www.newsite2.com \ / V \ /小时
}
]
[
{
链路1:HTTP:\ / \ / xyz3.com \ /获取\ / A,
链接2:HTTP:\ / \ / www.anotherwebsite3.com \ / C \ / T,
LINK3:HTTP:\ / \ / www.newsite3.com \ / V \ /小时
}
]
]
}
}
VAR数据= $ .parseJSON(dbData);
我能够访问click_title和commonlink使用此: -
data.click_title
data.links ['commonlink']
但不能访问链接1,链接2,LINK3。 我试过
data.links ['alllinks'],返回[对象的对象。
如果我尝试
data.links ['alllinks'] ['链接1']我得到了一个未定义
结构是非常奇怪的。 alllinks
是一个对象数组的数组(请注意 [
和第二 [
在alllinks:
,每个这样的开始的数组)。为了获得在第一的链接1
在第一的数组,你会怎么做:
VAR链接= data.links.alllinks [0] [0] .link1;
它更容易看到,如果你一直格式化你的JSON。在这里,我用 http://jsonlint.com 把它清理干净,然后我添加了一些注释(不该意见是有效的JSON):
{
click_title:GO,
链接: {
commonlink:http://bookings.com,
alllinks:[//&所述; ==启动外阵列
[//< ==开始一个内部数组
{//< ==启动对象的第一个条目
链路1:http://xyz1.com/get/a
链接2:http://www.anotherwebsite1.com/c/t
LINK3:http://www.newsite1.com/v/h
},
{//< ==启动对象,这是第二个项目
链路1:http://xyz2.com/get/a
链接2:http://www.anotherwebsite2.com/c/t
LINK3:http://www.newsite2.com/v/h
}
],//&所述; ==完第一内阵列
[//&所述; ==启动第二内阵列
{
链路1:http://xyz3.com/get/a
链接2:http://www.anotherwebsite3.com/c/t
LINK3:http://www.newsite3.com/v/h
}
] //&其中; ==完第二内阵列
] //< ==结束外阵列
}
}
因此,在总,你有三个链接1
的:在的一个对象[0] [0]
在 [0] [1]
,以及一个在对象上的 [1] [0]
。
I've parsed the json string and I am trying to access the elements. I am unable to access 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);
I am able to access click_title and commonlink using this :-
data.click_title
data.links['commonlink']
but not able to access link1, link2, link3. I tried
data.links['alllinks'] which returns [object Object].
If I try
data.links['alllinks']['link1'] i get undefined
The structure is very odd. alllinks
is an array of arrays of objects (note the [
and second [
after "alllinks":
, each of those starts an array). To get at the first link1
in the first array, you'd do:
var link = data.links.alllinks[0][0].link1;
It's easier to see that if you format your JSON consistently. Here I've used http://jsonlint.com to clean it up, and then I've added some comments (not that comments are valid in 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
}
}
So in total, you have three link1
's: The one on the object at [0][0]
, the one on the object at [0][1]
, and the one on the object at [1][0]
.
这篇关于&QUOT;对象的对象&QUOT;或在访问JSON元素未定义返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!