我有一个像这样的JSON字符串:
{"time":"2011-11-30 04:44","countryName":"Austria","sunset":"2011-11-30 16:32","rawOffset":1,"dstOffset":2,"countryCode":"AT","gmtOffset":1,"lng":10.2,"sunrise":"2011-11-30 07:42","timezoneId":"Europe/Vienna","lat":47.01}
我如何使用javascript解析这个?我尝试过使用:
function callbackFun(data) {
$j.each(data.result, function(i, item) {
alert(this.time);
});
}
但似乎这不正确。
答案 0 :(得分:5)
如果您从$.ajax()检索该数据,则可以设置dataType: 'json'
以自动为您解析。
否则只需使用$.parseJSON()
答案 1 :(得分:0)
如果你正在使用jQuery,那很简单:
var obj = '{"time":"2011-11-30 04:44","countryName":"Austria","sunset":"2011-11-30 16:32","rawOffset":1,"dstOffset":2,"countryCode":"AT","gmtOffset":1,"lng":10.2,"sunrise":"2011-11-30 07:42","timezoneId":"Europe/Vienna","lat":47.01}';
var json = jQuery.parseJSON(obj);
alert(json.time);
alert(json.countryName);
答案 2 :(得分:0)
你在找这个吗?
var MyJson = '{"time":"2011-11-30 04:44","countryName":"Austria","sunset":"2011-11-30 16:32","rawOffset":1,"dstOffset":2,"countryCode":"AT","gmtOffset":1,"lng":10.2,"sunrise":"2011-11-30 07:42","timezoneId":"Europe/Vienna","lat":47.01}';
var MyObject = jQuery.parseJSON(MyJson);