2010-10-31 78 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