1

I have a JSON string like this:

{"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}

How can I parse this using javascript? I have tried using:

function callbackFun(data) {
        $j.each(data.result, function(i, item) {
            alert(this.time);
        });
    }

But it seems this is not correct.

0

3 Answers 3

5

If you retrieve that piece of data from $.ajax() then you could set up dataType: 'json' to get it automatically parsed for you.

Otherwise just use $.parseJSON()

Sign up to request clarification or add additional context in comments.

Comments

0

If you're using jQuery, it's trivial:

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/

1 Comment

It's also trivial if you're not using jQuery.
0

Are you looking for this?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.