user950906
user950906

Reputation: 27

Using JSON to get value

I want to select one specific property of a jason object.

This is the structure of the 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"
  }
}

This is what I'm doing to get the 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>');
                });

I'm new with JSON so my question is: How do I get the latitude and longitude of a location by entering an artist name out this JSON object? Any Idea?

Upvotes: 1

Views: 168

Answers (2)

Sinetheta
Sinetheta

Reputation: 9449

If you have to match a given property value, then you're going to have to crawl and check for it.

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

Upvotes: 0

ShaneBlake
ShaneBlake

Reputation: 11096

You would use entry.location.lng and entry.location.lat inside your $.each() function.

each property, if not an array, can be accessed with dot notation, which most people find easier to read and understand. meaning, your $.each() could also be defined like this :

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

Hope that helps...

Upvotes: 1

Related Questions