通过JSON数据循环并获取事件信息


 {
"events": [
    {
        "summary": {
            "total_items": 2135,
            "first_event": 5242051116,
            "last_event": 5242051116,
            "filters": {
                "keywords": "Atlanta"
            },
            "num_showing": 1
        }
    },
    {
        "event": {
            "box_header_text_color": "FFFFFF",
            "locale": "en_US",
            "link_color": "990066",
            "box_background_color": "FFFFFF",
            "box_border_color": "FFE5F6",
            "timezone": "America/New_York",
            "organizer": {
                "url": "http://newyearsevedanceparty.eventbrite.com",
                "description": "We organize Swing, Latin & Ballroom Dinner & Dance events in and around the Gwinnett area.rn"Bringing the Joy of Dance to you One Step at a Time" rnhttp://www.dancing4fun.comhttp://www.meetup.com/dancing4funrnSean-christopher770.855.4396",
                "long_description": "We organize Swing, Latin & Ballroom Dinner & Dance events in and around the Gwinnett area.rn"Bringing the Joy of Dance to you One Step at a Time" rnhttp://www.dancing4fun.comhttp://www.meetup.com/dancing4funrnSean-christopher770.855.4396",
                "id": 249877987,
                "name": "Dancing4fun Dinner Dancing & Socializing"
            },
            "background_color": "FFE5F6",
            "id": 5242051116,
            "category": "music,entertainment",
            "box_header_background_color": "A2B8BF",
            "capacity": 0,
            "num_attendee_rows": null,
            "title": "Two Valentines Party in Atlanta!Valentines Day & Valentines Weekend Latin Dance party Atlanta Ga Thursday February 14, 2013 & Saturday February 16, 2013",
            "start_date": "2014-02-15 21:00:00",
            "status": "Live",
            "description": "<P STYLE="text-align: center;"><SPAN STYLE="color: #ff0000;"><STRONG><SPAN STYLE="font-size: large;">Two Valentines Parties in Atlanta!!<BR>Hosted by Dancing4fun!<BR><BR>Valentines Day & Valentines Weekend Latin Dance party </SPAN></STRONG></SPAN><BR><SPAN STYLE="color: #ff0000;"><STRONG><SPAN STYLE="font-size: large;">Atlanta Ga <BR>Thursday February 14, 2013<BR>And<BR>Saturday February 16, 2013<BR><BR></SPAN></STRONG></SPAN><IMG SRC="https://evbdn.eventbrite.com/s3-s3/eventlogos/1669518/facebookcoverlatin.jpg" ALT="" WIDTH="519" HEIGHT="132"></P>rn<P></P>",
            "end_date": "2014-02-16 03:00:00",
            "tags": "Valentines Day party Atlanta Ga, Valentines Latin Dance party Gwinnett, Valentines Atlanta Ga, Valentines Day Dance party Suwanee Ga, latin dance Valentines Day party Gwinnett, Suwanee Dinner and Dance Valentines party, Romantic Valentines Day event atlan",
            "timezone_offset": "GMT-0500",
            "text_color": "990066",
            "repeat_schedule": "custom-5226372",
            "title_text_color": "",
            "tickets": [
                {
                    "ticket": {
                        "description": "",
                        "end_date": "2014-02-15 20:00:00",
                        "min": 1,
                        "max": null,
                        "price": "11.24",
                        "visible": "true",
                        "currency": "USD",
                        "display_price": "10.00",
                        "type": 0,
                        "id": 16707094,
                        "include_fee": "false",
                        "name": "Thursday February 14, 2013 - Valentines Day Latin Night - Pay @ door"
                    }
                },
                {
                    "ticket": {
                        "description": "",
                        "end_date": "2014-02-15 20:00:00",
                        "min": 1,
                        "max": null,
                        "price": "11.24",
                        "visible": "true",
                        "currency": "USD",
                        "display_price": "10.00",
                        "type": 0,
                        "id": 16707098,
                        "include_fee": "false",
                        "name": "Valentines Weekend Latin Night  - Saturday February 16, 2013 - pay @ door"
                    }
                }
            ],
            "distance": "0.00M",
            "created": "2013-01-14 23:02:15",
            "url": "http://latindancingvalentinesatlanta.eventbrite.com/?aff=SRCH",
            "box_text_color": "A2B8BF",
            "privacy": "Public",
            "modified": "2013-12-07 15:54:30",
            "repeats": "yes"
        }
    }
]

}

当我击中EventBrite API时,我将获得此JSON格式化数据。我正在尝试使用jQuery说每个事件的标题。

尝试了很多事情,例如

.done(function (response) {
    $.each(response.events, function(index, element){
        console.log(element['event'].title);
    });
});

但我得到TypeError: obj is undefined length = obj.length

任何帮助将不胜感激。谢谢

我知道这里有很多示例,但我尝试了它们,但没有用于我的数据结构。

并非所有的节点都有事件属性,因此对于这些节点来说,它一定会失败。您必须先检查现有的节点,否则会收到对象参考错误。例如:

$.each(response.events, function(index, element){
    if(element['event']) {
        console.log(element['event'].title);
    }
});

请参阅此处的工作:http://jsfiddle.net/edv2q/

这是因为第一个元素:

    {
        "summary": {
            "total_items": 2135,
            "first_event": 5242051116,
            "last_event": 5242051116,
            "filters": {
                "keywords": "Atlanta"
            },
            "num_showing": 1
        }
    }

不包含"事件"属性...

最新更新