循环访问JSON数据并获取事件信息

时间:2014-01-23 12:29:21

标签: jquery 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.\r\n\"Bringing the Joy of Dance to you One Step at a Time\" \r\nhttp://www.dancing4fun.comhttp://www.meetup.com/dancing4fun\r\nSean-christopher770.855.4396",
                "long_description": "We organize Swing, Latin & Ballroom Dinner & Dance events in and around the Gwinnett area.\r\n\"Bringing the Joy of Dance to you One Step at a Time\" \r\nhttp://www.dancing4fun.comhttp://www.meetup.com/dancing4fun\r\nSean-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>\r\n<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

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

我知道这里有很多例子,但是我尝试过它们并没有为我的数据结构工作。

2 个答案:

答案 0 :(得分:1)

并非所有节点都具有事件属性,因此对于这些节点,它必然会失败。您必须首先检查存在的节点,否则您将收到对象引用错误。如:

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

在此处查看此工作:http://jsfiddle.net/eDV2Q/

答案 1 :(得分:1)

这是因为第一个元素:

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

不包含“event”属性......