admin管理员组

文章数量:1323678

I am trying to convert JSON object data from which contains to JavaScript object `

{
    "base": "USD",
    "date": "2016-12-14",
    "rates": {
        "AUD": 1.3319,
        "BGN": 1.8375,
        "BRL": 3.311,
        "CAD": 1.3116,
        "CHF": 1.0097,
        "CNY": 6.9052,
        "CZK": 25.388,
        "DKK": 6.986,
        "GBP": 0.78883,
        "HKD": 7.7566,
        "HRK": 7.0843,
        "HUF": 295.84,
        "IDR": 13288,
        "ILS": 3.8097,
        "INR": 67.479,
        "JPY": 114.98,
        "KRW": 1166,
        "MXN": 20.262,
        "MYR": 4.4441,
        "NOK": 8.4764,
        "NZD": 1.3849,
        "PHP": 49.716,
        "PLN": 4.1716,
        "RON": 4.2421,
        "RUB": 61.197,
        "SEK": 9.1651,
        "SGD": 1.424,
        "THB": 35.59,
        "TRY": 3.4879,
        "ZAR": 13.67,
        "EUR": 0.9395
    }
}

What i have tried so far in console

var text = $.getJSON('')
var obj = JSON.parse(text);

which gives me an error.

var text = $.getJSON('')
var obj = JSON.stringify(text, 0, 2)

which turn everything to string which is not what i wanted.

I am trying to achieve(after manage to convert them into object)

obj.rates.AUD

Which will return the value 1.3319 which is from the JSON object data. Thanks

I am trying to convert JSON object data from http://api.fixer.io/latest?base=USD which contains to JavaScript object `

{
    "base": "USD",
    "date": "2016-12-14",
    "rates": {
        "AUD": 1.3319,
        "BGN": 1.8375,
        "BRL": 3.311,
        "CAD": 1.3116,
        "CHF": 1.0097,
        "CNY": 6.9052,
        "CZK": 25.388,
        "DKK": 6.986,
        "GBP": 0.78883,
        "HKD": 7.7566,
        "HRK": 7.0843,
        "HUF": 295.84,
        "IDR": 13288,
        "ILS": 3.8097,
        "INR": 67.479,
        "JPY": 114.98,
        "KRW": 1166,
        "MXN": 20.262,
        "MYR": 4.4441,
        "NOK": 8.4764,
        "NZD": 1.3849,
        "PHP": 49.716,
        "PLN": 4.1716,
        "RON": 4.2421,
        "RUB": 61.197,
        "SEK": 9.1651,
        "SGD": 1.424,
        "THB": 35.59,
        "TRY": 3.4879,
        "ZAR": 13.67,
        "EUR": 0.9395
    }
}

What i have tried so far in console

var text = $.getJSON('http://api.fixer.io/latest?base=GBP')
var obj = JSON.parse(text);

which gives me an error.

var text = $.getJSON('http://api.fixer.io/latest?base=GBP')
var obj = JSON.stringify(text, 0, 2)

which turn everything to string which is not what i wanted.

I am trying to achieve(after manage to convert them into object)

obj.rates.AUD

Which will return the value 1.3319 which is from the JSON object data. Thanks

Share edited Dec 14, 2016 at 20:09 msanford 12.2k13 gold badges71 silver badges98 bronze badges asked Dec 14, 2016 at 19:15 puterputer 1131 gold badge2 silver badges12 bronze badges 3
  • 3 "which gives me an error." What's the error? – Eric Commented Dec 14, 2016 at 19:18
  • I ran into an http/https error at first, you may want to drop the protocol. – wyldstallyns Commented Dec 14, 2016 at 19:20
  • Please read the documentation: api.jquery./jquery.getjson . – Felix Kling Commented Dec 14, 2016 at 19:55
Add a ment  | 

3 Answers 3

Reset to default 3

jQuery's getJSON parses the JSON for you, but the request is asynchronous, so it doesn't return the response text directly; instead it returns a jqXHR object.

This should work for you:

$.getJSON('http://api.fixer.io/latest?base=GBP').then( function ( obj ) {
  console.log( obj );
} );

If you just look at the response of that request, you can see that the JSON es inside of the responseText property. Just do:

var obj = JSON.parse(text.responseText);

Additionally, getJson is an asynchronous call, so you need to do this in the callback otherwise the response may not exist yet:

var obj;
var text = $.getJSON('http://api.fixer.io/latest?base=GBP', function() {
  obj = JSON.parse(text.responseText);
});

Edit: As menter pointed out, you can also just get the JSON straight from the responseJSON property:

var obj;
var text = $.getJSON('http://api.fixer.io/latest?base=GBP', function(){
   obj = text.responseJSON;
});

Don't use JSON.parse() or JSON.stringify() . As demonstrated in jquery docs $.getJSON(url , callback) takes a callback . Also $.getJSON() uses $.parseJSON() behind the scenes so an object will be passed into the callback by default .

Your Problem..

var text = $.getJSON('http://api.fixer.io/latest?base=GBP') <-- returns nothing

The Solution .. add a callback function

var text = $.getJSON('http://api.fixer.io/latest?base=GBP' , function(response){

console.log(response.rates.AUD);

})

docs : http://api.jquery./jquery.getjson/

Hope this helps.

本文标签: How to convert JSON object to JavaScript objectStack Overflow