簡體   English   中英

JSON解析問題

[英]JSON parse issue

我有一個像這樣的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM