JSON解析问题

JSON parse issue

本文关键字:问题 JSON      更新时间:2023-09-26

我有一个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);
        });
    }

如果您从$.ajax()中检索那块数据,那么您可以设置dataType: 'json'为您自动解析它。

否则使用$.parseJSON()

如果您正在使用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);
http://api.jquery.com/jQuery.parseJSON/

你在找这个吗?

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);