是否有正式文件解释函数参数的工作原理?
$.getJSON("files/golfClubs.json", function (data) {
$.each(data, function (index, value) {
$("#filtermenu").append("<option value='" + data[0].GolfCourses[index].GolfCourseID + "'>" + data[0].GolfCourses[index].GolfCourseName + " </option>");
});
});
比如说:
$.getJSON("files/golfClubs.json", function (data) {
alert(data);
//Returns undefined. Unless .JSON.stringify-ed().
});
问题1:我想知道当你引用相同的参数变量时,参数如何基本上传递数据。特别是嵌套的jSON。传递2个参数而不是1个参数会发生什么?
问题2:与函数中的参数一起使用下面的JSON,如何访问GolfCourses.GolfCourseBookings.DayBookings?
[
{
"GolfClubID": "TROPICANA",
"GolfClubName": "Tropicana Golf and Country Club",
"GolfCourses": [
{
"GolfCourseID": "1",
"GolfCourseName": "West Course - 1st 9",
"GolfCourseBookings": [
{
"DayNumber": 1,
"DayDate": "19/03/2014",
"DayBookings": [
{
"TimeSlotID": "0",
"Time" : "07:00",
"Class": "Closed"
},
{
"TimeSlotID": "1",
"Time" : "07:10",
"Class": "Closed"
},
{
"TimeSlotID": "2",
"Time" : "07:20",
"Class": "Closed"
},
答案 0 :(得分:0)
请阅读http://api.jquery.com/jquery.getjson/
你所拥有的是一系列对象(数据),你可以这样说,因为在javascript:
[] =数组, {} = object
例如:
var GolfCourses = []; // Array
var GolfCourses = new Array(); // Array
var GolfCourse = {}; // Object
var GolfCourse = new Object(); // Object
所以回答你的问题:
Q1。 $ .getJSON返回内部的所有内容&#34;数据&#34;。因此,如果您想从服务器发送2个参数,例如name =&#34; John&#34;和last_name =&#34; Travolta&#34;你用javascript对象表示法(JSON)发送它:
data = { "name": "John", "last_name": "Travolta" };
在客户端,您可以像这样访问它:
data.name; // John
data.last_name; // Travolta
如果您希望服务器发送各种对象:
data = [
{ "name": "John",
"last_name": "Travolta" },
{ "name": "James",
"last_name": "Dean" }
];
您可以访问:
data[1].last_name; // Dean
Q2。基本上是:
data[0].GolfCourses[0].GolfCouseBookings[0].DayBookings[0].Class; // Closed
Google&#34; JSON或Javascript Object Notation&#34;信息。