使用JSON获取价值

时间:2012-03-02 19:34:11

标签: javascript jquery json

我想选择jason对象的一个​​特定属性。

这是JSON的结构:

{
  "resultsPage:" {
    "results": { "event": [
      {
        "id":11129128,
        "type":"Concert",
        "uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
        "displayName":"Wild Flag at The Fillmore (April 18, 2012)",
        "start":{"time":"20:00:00",
                 "date":"2012-04-18",
                 "datetime":"2012-04-18T20:00:00-0800"},
        "performance":[{"artist":{"uri":"http://www.songkick.com/artists/29835-wild-flag?utm_source=PARTNER_ID&utm_medium=partner",
                                  "displayName":"Wild Flag","id":29835,"identifier":[]},
                        "displayName":"Wild Flag",
                        "billingIndex":1,
                        "id":21579303,
                        "billing":"headline"}],
        "location":{"city":"San Francisco, CA, US","lng":-122.4332937,"lat":37.7842398},
        "venue":{"id":6239,
                 "displayName":"The Fillmore",
                 "uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
                 "lng":-122.4332937, "lat":37.7842398,
                 "metroArea":{"uri":"http://www.songkick.com/metro_areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
                              "displayName":"SF Bay Area","country":{"displayName":"US"},"id":26330,"state":{"displayName":"CA"}}},
        "status":"ok",
        "popularity":0.012763
      }, ....
    ]},
    "totalEntries":24,
    "perPage":50,
    "page":1,
    "status":"ok"
  }
}

这就是我正在做的获取JSON:

url: "http://api.songkick.com/api/3.0/artists/480448/calendar.json?apikey=APIKEY&jsoncallback=?",
            dataType: "jsonp", // <== JSON-P request
            success: function (data) {
                $.each(data["resultsPage"]["results"]["event"], function (i, entry) {
                    $("#events").append('<li><a href="' + entry.uri + '">' + entry.displayName + '</a></li>');
                });

我是JSON的新手,所以我的问题是:如何通过输入此JSON对象的艺术家名称来获取位置的纬度和经度?有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您可以在entry.location.lng函数中使用entry.location.lat$.each()

每个属性(如果不是数组)都可以使用点表示法访问,大多数人都会发现它更容易阅读和理解。意思是,您的$.each()也可以这样定义:

$.each(data.resultsPage.results.event, function (i, entry) {

希望有帮助...

答案 1 :(得分:0)

如果您必须匹配给定的属性值,那么您将不得不抓取并检查它。

for ( loop events )
    if ( select Name = thing you want )
        do my stuff