【问题标题】:Grabbing certain values in this JSON file from the Meetup API doesn't work从 Meetup API 获取此 JSON 文件中的某些值不起作用
【发布时间】:2013-11-04 23:19:40
【问题描述】:

我正在使用 Meetup API。一切似乎都有效,除了我无法从 JSON 中获取 item.venue.zip 或 item.venue.address_1 的值。

这是我的代码:

$(document).ready(function(){ 
$.getJSON("http://api.meetup.com/2/open_events.json?zip=10001&radius=7&topic=technology&status=upcoming&time=,1w&key=APIKEY&callback=?", function (data) { 
var htmlString = ""; 
$.each(data.results, function (i, item) {
 htmlString += '<h3><a href="' + item.event_url + '" target="_blank">' + item.name + '</a></h3>' + 'People attending: ' + item.yes_rsvp_count + item.time + item.group.name + item.maybe_rsvp_count; }); 
$('#upcoming').html(htmlString);
}); 
});

这就是我的 JSON 的样子:

?({
"results": [
    {
        "rsvp_limit": 90,
        "status": "upcoming",
        "visibility": "public",
        "maybe_rsvp_count": 0,
        "venue": {
            "id": 15570612,
            "zip": "11215",
            "lon": -73.99025,
            "repinned": false,
            "name": "SNAP Interactive (4th Floor)",
            "state": "NY",
            "address_1": "462 7th Avenue",
            "lat": 40.751926,
            "city": "New York",
            "country": "us"
        },
        "id": "146849412",
        "utc_offset": -14400000,
        "distance": 0.13401024043560028,
        "time": 1382742000000,
        "waitlist_count": 0,
        "updated": 1382447374000,
        "yes_rsvp_count": 83,
        "created": 1382446781000,
        "event_url": "http:\/\/www.meetup.com\/nygraph\/events\/146849412\/",
        "description": "<p><b>For this week’s meetup, Colin Hodge will be speaking about his experiences building Bang With Friends’ recommendation engine and transitioning it over to one using Neo4j, a graph database.<\/b><\/p>\n<p>Launched in January, Bang With Friends has been one of the fastest-growing Facebook apps of this year, scaling rapidly from a hastily thrown-together half-joke to over half a million users in its first two weeks, and in the process becoming a serious player in the dating and social ecosystem.<\/p>\n<p>Colin Hodge co-founded Bang With Friends in January 2013 and has served as CEO since its conception. Previously, Colin founded HeardAboutYou, a dating startup that evolved into Bang With Friends, as well as Cloud 8 Studios, a mobile apps company that published flick+share, a location-based photo sharing app. Colin holds a Bachelor of Science from Cornell University in Computer Science, where he served as Vice President of the local chapter of the Association for Computing Machinery.<\/p>",
        "name": "Graph-based Recommendations at Bang With Friends",
        "headcount": 0,
        "group": {
            "id": 1876871,
            "group_lat": 40.75,
            "name": "NY Graph Meetup",
            "group_lon": -73.98999786376953,
            "join_mode": "open",
            "urlname": "nygraph",
            "who": "Members"
        }
    }
],
"meta": {
    "lon": "\"\"",
    "count": 1,
    "link": "http:\/\/api.meetup.com\/2\/open_events.json",
    "next": "",
    "total_count": 1,
    "url": "http:\/\/api.meetup.com\/2\/open_events.json?key=APIKEY&status=upcoming&radius=1.0&topic=technology&and_text=False&limited_events=False&desc=False&offset=0&callback=%3F&format=json&zip=10001&page=200&time=%2C1d",
    "id": "",
    "title": "Meetup Open Events v2",
    "updated": 1382724786664,
    "description": "Searches for recent and upcoming public events hosted by Meetup groups. Its search window  is the past one month through the next three months, and is subject to change. Open Events is optimized to search for current events by location, category, topic, or text, and only lists Meetups that have **3 or more RSVPs**. The number or results returned with each request is not guaranteed to be the same as the page size due to secondary filtering. If you're looking for a particular event or events within a particular group, use the standard [Events](\/meetup_api\/docs\/2\/events\/) method.",
    "method": "OpenEvents",
    "lat": "\"\""
}
})

同样,抓取 item.status 或 item.name 或 item.group.name 都可以正常工作,但一旦尝试抓取 item.venue.zip,我就会在控制台中收到类似这样的错误:

未捕获的类型错误:无法读取未定义的属性“zip”

什么给了?

【问题讨论】:

  • 您发布的 JSON 是您从 API 收到的实际响应吗?
  • 是的,这是我从 Meetup API 获得的实际响应的一部分。
  • 看起来 JSON 搞砸了。 “结果”以数组开头,但以 } 结尾。当我遇到这样的问题时,它有助于控制台在 Firefox/Chrome 中记录响应,它将允许您遍历对象/数组。
  • 哦,呃……那是因为我在某个时候把它剪掉了。让我编辑帖子以减少选择。
  • @Trendy 你能解释一下如何在 Firefox/Chrome 中控制台记录响应吗?例如,console.log(item.name) 不起作用。

标签: javascript jquery json api meetup


【解决方案1】:

更新: 我发现所有数据项都不存在“地点”属性(即数据不完整),所以我必须应用一个

 if (item.venue)

到代码。

另外,我下载了一个 chrome 扩展,当我在浏览器中查看 JSON 文件时,它有助于更​​好地解析 JSON。

:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2023-03-09
    • 2019-12-03
    相关资源
    最近更新 更多