2010-10-31 86 views
3

我想解析一個JSON響應使用jQuery從last.fm和獲取經度和緯度值。持有這些值的JSON的部分看起來像這樣:如何解析JSON,如geo:point?

"location":{"geo:point":{"geo:lat":"58.409901","geo:long":"15.563306"} 

我得到的代碼是這樣的:

$.getJSON("http://ws.audioscrobbler.com/2.0/?method=artist.getevents&artist=familjen&api_key=xformat=json&callback=?", 
function(data){ 
    $.each(data.events.event, function(i,item){ 
    $("#menucontent").append("<br/>"+item.title + " " + item.venue.location.geo:point.geo:lat); 
     }); 
}); 

顯然,部分「item.venue.location.geo:point.geo:拉特「不起作用。我如何使用「:」獲取這些值?

回答

6

您可以使用bracket notation當標識符不能在dot notation表示,它會是這樣的:

item.venue.location["geo:point"]["geo:lat"] 

爲了說明,所有的這些都訪問同一件事的方法:

obj.thing.prop 
obj.thing["prop"] 
obj["thing"]["prop"] 
obj["thing"].prop