我想将我的主要JSON对象的一部分存储在一个变量中,以便我可以遍历它(例如,存储“卡片”,以便我可以遍历Govenors,课程和教师以获得“ title”和“ href” ',然后再与他们一起创建元素)。目前我正在做:
var content = $.getJSON("website-contents.json");
var place = content["cards"];
当我在Chrome开发工具中调用content变量时,它返回我的主对象,但是当我调用“ place”变量时,它仅返回未定义。我也曾尝试过var place = content.cards
,var place = content[0]
但似乎无济于事。这是我的JSON(长度表示歉意)
{
"accordian" : {
"faqs" : {
"head" : {
"answer" : "Simon Firth",
"question" : "Whos the head?"
},
"location" : {
"answer" : "Wylye Bulding, Wiltshire College",
"question" : "Where are we based?"
}
}
},
"article" : {
"new-site" : {
"Title" : "Launch of new website!",
"author" : "Alex Shaw",
"content" : "We now have a new website due to the amazing computing students!",
"hashbang" : "newSite"
},
"test" : {
"author" : "A Shaw",
"content" : "This is a test article",
"hashbang" : "test",
"title" : "Test Article"
}
},
"cards" : {
"govenors":{
"title":"Govenors",
"href":"#govenors",
"abanks":{
"name":"Alec Banks"
},
"nowen":{
"name":"Neil Owen"
}
},
"courses" : {
"title":"Courses",
"href":"#courses",
"Art" : {
"teacher" : "Mrs Bellars",
"title" : "Art"
},
"Biology" : {
"teacher" : "Mrs Miller",
"title" : "Biology"
},
"Computing" : {
"teacher" : "Mr Chambers",
"title" : "Computing"
},
"Further Maths" : {
"teacher" : "Mr Grimsley",
"title" : "Further Maths"
},
"Maths" : {
"teacher" : "Mr Grimsley",
"title" : "Maths"
}
},
"teachers" : {
"title":"Teachers",
"href":"#teachers",
"cchambers" : {
"email" : "email1",
"name" : "Craig Chambers"
},
"kgrimsley" : {
"email" : "email2",
"name" : "Kevin Grimsley"
},
"sfirth" : {
"email" : "email3",
"name" : "Simon Firth"
}
}
}
}
getJSON()
是异步的。您需要向它传递一个成功处理程序,然后解析出数据:
var place,
content = $.getJSON("website-contents.json", function(data) {
place = data.cards;
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句