将JSON数据转换为时间表

时间:2017-03-26 11:07:05

标签: javascript html json datatable

我有一个来自URL的大型JSON文件的API,我正在尝试做的是创建一个时间表。我已经得到了能够打印JSON文件的某些部分,但我似乎无法得到它需要的方式。以下是JSON的示例,这只是两天的健身课程:

{
"error": 0,
"msg": "Calendar data available",
"data": [
    {
        "daylabel": "Saturday 1st of April 2017",
        "datetime": 1490965200,
        "classdata": [
            {
                "id": "22728",
                "name": "TRX/Kettlebell",
                "leadbyuserid": "1395",
                "startdatetime": "1490985900",
                "enddatetime": "1490988600",
                "bookingtypeid": "88",
                "maxcount": "14",
                "shortname": "TRXK",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Gianluca Silvestrini",
                "classlabel": "TRX/Kettlebell",
                "displaylabel": "05:45am - 06:30am - Studio 2",
                "trainerlabel": "Trainer: Gianluca Silvestrini",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21367",
                "name": "Barre",
                "leadbyuserid": "695",
                "startdatetime": "1490988600",
                "enddatetime": "1490991300",
                "bookingtypeid": "6",
                "maxcount": "26",
                "shortname": "BARR",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "Angelique Maslen",
                "classlabel": "Barre",
                "displaylabel": "06:30am - 07:15am - Studio 1",
                "trainerlabel": "Trainer: Angelique Maslen",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21365",
                "name": "Streamline Body Sculpting™ ",
                "leadbyuserid": "1001",
                "startdatetime": "1490990400",
                "enddatetime": "1490993100",
                "bookingtypeid": "95",
                "maxcount": "18",
                "shortname": "SBS",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Tim Heasman",
                "classlabel": "Streamline Body Sculpting™ ",
                "displaylabel": "07:00am - 07:45am - Studio 2",
                "trainerlabel": "Trainer: Tim Heasman",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21366",
                "name": "Small Group Circuit Training",
                "leadbyuserid": "1395",
                "startdatetime": "1490990400",
                "enddatetime": "1490992800",
                "bookingtypeid": "71",
                "maxcount": "14",
                "shortname": "SGCT",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Gianluca Silvestrini",
                "classlabel": "Small Group Circuit Training",
                "displaylabel": "07:00am - 07:40am - Main Floor",
                "trainerlabel": "Trainer: Gianluca Silvestrini",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21364",
                "name": "Aerial Foundations",
                "leadbyuserid": "695",
                "startdatetime": "1490992200",
                "enddatetime": "1490994900",
                "bookingtypeid": "108",
                "maxcount": "18",
                "shortname": "AFIT",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "Angelique Maslen",
                "classlabel": "Aerial Foundations",
                "displaylabel": "07:30am - 08:15am - Studio 1",
                "trainerlabel": "Trainer: Angelique Maslen",
                "isfull": 1,
                "styledata": "background-color:#666;color:#fff;"
            },
            {
                "id": "21363",
                "name": "Kettlebell Circuit ",
                "leadbyuserid": "1001",
                "startdatetime": "1490994000",
                "enddatetime": "1490996400",
                "bookingtypeid": "99",
                "maxcount": "18",
                "shortname": "KBL",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Tim Heasman",
                "classlabel": "Kettlebell Circuit ",
                "displaylabel": "08:00am - 08:40am - Main Floor",
                "trainerlabel": "Trainer: Tim Heasman",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21362",
                "name": "Mat Pilates",
                "leadbyuserid": "1373",
                "startdatetime": "1490994900",
                "enddatetime": "1490997600",
                "bookingtypeid": "89",
                "maxcount": "18",
                "shortname": "MATP",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Kylie Tabrett",
                "classlabel": "Mat Pilates",
                "displaylabel": "08:15am - 09:00am - Studio 2",
                "trainerlabel": "Trainer: Kylie Tabrett",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21361",
                "name": "TRX/Pilates",
                "leadbyuserid": "16",
                "startdatetime": "1490995800",
                "enddatetime": "1490998500",
                "bookingtypeid": "97",
                "maxcount": "20",
                "shortname": "TRXP",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "John Rahme",
                "classlabel": "TRX/Pilates",
                "displaylabel": "08:30am - 09:15am - Studio 1",
                "trainerlabel": "Trainer: John Rahme",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21360",
                "name": "Small Group Cardio",
                "leadbyuserid": "15",
                "startdatetime": "1490997000",
                "enddatetime": "1490999400",
                "bookingtypeid": "100",
                "maxcount": "14",
                "shortname": "SMGC",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Lyndsay Flight",
                "classlabel": "Small Group Cardio",
                "displaylabel": "08:50am - 09:30am - Main Floor",
                "trainerlabel": "Trainer: Lyndsay Flight",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "26080",
                "name": "40 Day Focus Measurements",
                "leadbyuserid": "16",
                "startdatetime": "1490997600",
                "enddatetime": "1491000900",
                "bookingtypeid": "124",
                "maxcount": "20",
                "shortname": "MEAS",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "0",
                "trainername": "John Rahme",
                "classlabel": "40 Day Focus Measurements",
                "displaylabel": "09:00am - 09:55am",
                "trainerlabel": "Trainer: John Rahme",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21359",
                "name": "Restorative Yoga",
                "leadbyuserid": "1094",
                "startdatetime": "1490998500",
                "enddatetime": "1491002100",
                "bookingtypeid": "84",
                "maxcount": "20",
                "shortname": "RSYO",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Jennifer Vo",
                "classlabel": "Restorative Yoga",
                "displaylabel": "09:15am - 10:15am - Studio 2",
                "trainerlabel": "Trainer: Jennifer Vo",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21357",
                "name": "Barre",
                "leadbyuserid": "15",
                "startdatetime": "1491000300",
                "enddatetime": "1491003000",
                "bookingtypeid": "6",
                "maxcount": "26",
                "shortname": "BARR",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "Lyndsay Flight",
                "classlabel": "Barre",
                "displaylabel": "09:45am - 10:30am - Studio 1",
                "trainerlabel": "Trainer: Lyndsay Flight",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21358",
                "name": "Mat Pilates",
                "leadbyuserid": "1373",
                "startdatetime": "1491000300",
                "enddatetime": "1491003000",
                "bookingtypeid": "119",
                "maxcount": "12",
                "shortname": "MATP",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Kylie Tabrett",
                "classlabel": "Mat Pilates",
                "displaylabel": "09:45am - 10:30am - Main Floor",
                "trainerlabel": "Trainer: Kylie Tabrett",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21356",
                "name": "TRX/Pilates Studio 2",
                "leadbyuserid": "1395",
                "startdatetime": "1491003000",
                "enddatetime": "1491005700",
                "bookingtypeid": "98",
                "maxcount": "14",
                "shortname": "TRXP",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Gianluca Silvestrini",
                "classlabel": "TRX/Pilates Studio 2",
                "displaylabel": "10:30am - 11:15am - Studio 2",
                "trainerlabel": "Trainer: Gianluca Silvestrini",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21354",
                "name": "Streamline Body Sculpting™",
                "leadbyuserid": "1001",
                "startdatetime": "1491003900",
                "enddatetime": "1491006600",
                "bookingtypeid": "68",
                "maxcount": "28",
                "shortname": "SBS",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Tim Heasman",
                "classlabel": "Streamline Body Sculpting™",
                "displaylabel": "10:45am - 11:30am - Main Floor",
                "trainerlabel": "Trainer: Tim Heasman",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "27553",
                "name": "Detox Launch Seminar",
                "leadbyuserid": "16",
                "startdatetime": "1491004800",
                "enddatetime": "1491008100",
                "bookingtypeid": "127",
                "maxcount": "50",
                "shortname": "DTOX",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "John Rahme",
                "classlabel": "Detox Launch Seminar",
                "displaylabel": "11:00am - 11:55am - Studio 1",
                "trainerlabel": "Trainer: John Rahme",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "24513",
                "name": "Gentle Flow Yoga",
                "leadbyuserid": "921",
                "startdatetime": "1491006600",
                "enddatetime": "1491010200",
                "bookingtypeid": "92",
                "maxcount": "20",
                "shortname": "GFYO",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Denise Sorrell",
                "classlabel": "Gentle Flow Yoga",
                "displaylabel": "11:30am - 12:30pm - Studio 2",
                "trainerlabel": "Trainer: Denise Sorrell",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            }
        ]
    },
    {
        "daylabel": "Sunday 2nd of April 2017",
        "datetime": 1491051600,
        "classdata": [
            {
                "id": "21352",
                "name": "Barre",
                "leadbyuserid": "885",
                "startdatetime": "1491080400",
                "enddatetime": "1491083100",
                "bookingtypeid": "6",
                "maxcount": "26",
                "shortname": "BARR",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "Anna McLeod",
                "classlabel": "Barre",
                "displaylabel": "07:00am - 07:45am - Studio 1",
                "trainerlabel": "Trainer: Anna McLeod",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21351",
                "name": "Flow Yoga",
                "leadbyuserid": "921",
                "startdatetime": "1491081300",
                "enddatetime": "1491084900",
                "bookingtypeid": "81",
                "maxcount": "20",
                "shortname": "FYO",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Denise Sorrell",
                "classlabel": "Flow Yoga",
                "displaylabel": "07:15am - 08:15am - Studio 2",
                "trainerlabel": "Trainer: Denise Sorrell",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21350",
                "name": "Streamline Body Sculpting™",
                "leadbyuserid": "1342",
                "startdatetime": "1491084000",
                "enddatetime": "1491086700",
                "bookingtypeid": "68",
                "maxcount": "28",
                "shortname": "SBS",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "Rebecca Pearsall",
                "classlabel": "Streamline Body Sculpting™",
                "displaylabel": "08:00am - 08:45am - Studio 1",
                "trainerlabel": "Trainer: Rebecca Pearsall",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21349",
                "name": "Mat Pilates",
                "leadbyuserid": "885",
                "startdatetime": "1491084000",
                "enddatetime": "1491086700",
                "bookingtypeid": "119",
                "maxcount": "12",
                "shortname": "MATP",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Anna McLeod",
                "classlabel": "Mat Pilates",
                "displaylabel": "08:00am - 08:45am - Main Floor",
                "trainerlabel": "Trainer: Anna McLeod",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21348",
                "name": "Restorative Yoga",
                "leadbyuserid": "921",
                "startdatetime": "1491085800",
                "enddatetime": "1491089400",
                "bookingtypeid": "84",
                "maxcount": "20",
                "shortname": "RSYO",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Denise Sorrell",
                "classlabel": "Restorative Yoga",
                "displaylabel": "08:30am - 09:30am - Studio 2",
                "trainerlabel": "Trainer: Denise Sorrell",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21347",
                "name": "Small Group Circuit Training",
                "leadbyuserid": "1342",
                "startdatetime": "1491087600",
                "enddatetime": "1491090000",
                "bookingtypeid": "71",
                "maxcount": "14",
                "shortname": "SGCT",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Rebecca Pearsall",
                "classlabel": "Small Group Circuit Training",
                "displaylabel": "09:00am - 09:40am - Main Floor",
                "trainerlabel": "Trainer: Rebecca Pearsall",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21346",
                "name": "Barre",
                "leadbyuserid": "885",
                "startdatetime": "1491087600",
                "enddatetime": "1491090300",
                "bookingtypeid": "6",
                "maxcount": "26",
                "shortname": "BARR",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "Anna McLeod",
                "classlabel": "Barre",
                "displaylabel": "09:00am - 09:45am - Studio 1",
                "trainerlabel": "Trainer: Anna McLeod",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21345",
                "name": "Aerial Pilates ",
                "leadbyuserid": "1036",
                "startdatetime": "1491090600",
                "enddatetime": "1491093000",
                "bookingtypeid": "101",
                "maxcount": "11",
                "shortname": "APIL",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Tanya Watts",
                "classlabel": "Aerial Pilates ",
                "displaylabel": "09:50am - 10:30am - Studio 2",
                "trainerlabel": "Trainer: Tanya Watts",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21344",
                "name": "Semi-Private Kettlebell Circuit",
                "leadbyuserid": "1342",
                "startdatetime": "1491091200",
                "enddatetime": "1491093600",
                "bookingtypeid": "96",
                "maxcount": "14",
                "shortname": "SEMK",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Rebecca Pearsall",
                "classlabel": "Semi-Private Kettlebell Circuit",
                "displaylabel": "10:00am - 10:40am - Main Floor",
                "trainerlabel": "Trainer: Rebecca Pearsall",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21343",
                "name": "Mat Pilates ",
                "leadbyuserid": "885",
                "startdatetime": "1491091200",
                "enddatetime": "1491093900",
                "bookingtypeid": "2",
                "maxcount": "36",
                "shortname": "MATP",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "1",
                "trainername": "Anna McLeod",
                "classlabel": "Mat Pilates ",
                "displaylabel": "10:00am - 10:45am - Studio 1",
                "trainerlabel": "Trainer: Anna McLeod",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            },
            {
                "id": "21342",
                "name": "Aerial Yoga",
                "leadbyuserid": "1036",
                "startdatetime": "1491093900",
                "enddatetime": "1491096900",
                "bookingtypeid": "90",
                "maxcount": "11",
                "shortname": "AYO",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "2",
                "trainername": "Tanya Watts",
                "classlabel": "Aerial Yoga",
                "displaylabel": "10:45am - 11:35am - Studio 2",
                "trainerlabel": "Trainer: Tanya Watts",
                "isfull": 1,
                "styledata": "background-color:#666;color:#fff;"
            },
            {
                "id": "21341",
                "name": "Small Group Circuit Training",
                "leadbyuserid": "1342",
                "startdatetime": "1491094800",
                "enddatetime": "1491097200",
                "bookingtypeid": "71",
                "maxcount": "14",
                "shortname": "SGCT",
                "isactive": "1",
                "isbooked": "1",
                "screentypeid": "3",
                "trainername": "Rebecca Pearsall",
                "classlabel": "Small Group Circuit Training",
                "displaylabel": "11:00am - 11:40am - Main Floor",
                "trainerlabel": "Trainer: Rebecca Pearsall",
                "isfull": 0,
                "styledata": "background-color:#008c8c;color:#fff;"
            }
        ]
    }
]

}

我需要这样做,以便dayLabel对象成为包含它下面所有类的列,就像这样:http://quanticalabs.com/script/timetable-responsive-schedule-for-wordpress/7010836

获取对嵌套对象的访问权限以及创建正确的循环并以这种方式将它们吐出的任何帮助都会很棒,非常感谢。

1 个答案:

答案 0 :(得分:0)

要访问data数组,您可以尝试提取它,然后使用foreach循环进行迭代。

var jsondata = json.data

然后在foreach循环中创建一个包含dayLabel文本的div。还有一个新的foreach工作,将classdata放入列表中。

PS:对不起我的英语,这不是我的母语。