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
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With