I'm trying to parse a bit.ly JSON response in javscript.
I get the JSON via XmlHttpRequest.
var req = new XMLHttpRequest;
req.overrideMimeType("application/json");
req.open('GET', BITLY_CREATE_API + encodeURIComponent(url)
+ BITLY_API_LOGIN, true);
var target = this;
req.onload = function() {target.parseJSON(req, url)};
req.send(null);
parseJSON: function(req, url) {
if (req.status == 200) {
var jsonResponse = req.responseJSON;
var bitlyUrl = jsonResponse.results[url].shortUrl;
}
I do this in a firefox addon. When I run I get the error "jsonResponse is undefined" for the line var bitlyUrl = jsonResponse.results[url].shortUrl;
. Am I doing anything wrong in parsing JSON here? Or what is wrong with this code?
You can simply set
xhr.responseType = 'json';
Documentation for responseType
Use nsIJSON if this is for a FF extension:
For a webpage, just use
JSON.parse
instead ofComponents.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON.decode
New ways I:
fetch
TL;DR I'd recommend this way as long as you don't have to send synchronous requests or support old browsers.
A long as your request is asynchronous you can use the Fetch API to send HTTP requests. The fetch API works with promises, which is a nice way to handle asynchronous workflows in JavaScript. With this approach you use
fetch()
to send a request andResponseBody.json()
to parse the response:Compatibility: The Fetch API is not supported by IE11 as well as Edge 12 & 13. However, there are polyfills.
New ways II:
responseType
As Londeren has written in his answer, newer browsers allow you to use the
responseType
property to define the expected format of the response. The parsed response data can then be accessed via theresponse
property:Compatibility:
responseType = 'json'
is not supported by IE11.The classic way
The standard XMLHttpRequest has no
responseJSON
property, justresponseText
andresponseXML
. As long as bitly really responds with some JSON to your request,responseText
should contain the JSON code as text, so all you've got to do is to parse it withJSON.parse()
:Compatibility: This approach should work with any browser that supports
XMLHttpRequest
andJSON
.JSONHttpRequest
If you prefer to use
responseJSON
, but want a more lightweight solution than JQuery, you might want to check out my JSONHttpRequest. It works exactly like a normal XMLHttpRequest, but also provides theresponseJSON
property. All you have to change in your code would be the first line:JSONHttpRequest also provides functionality to easily send JavaScript objects as JSON. More details and the code can be found here: http://pixelsvsbytes.com/2011/12/teach-your-xmlhttprequest-some-json/.
Full disclosure: I'm the owner of Pixels|Bytes. I think my script is a good solution to the problem, so I posted it here. Please leave a comment, if you want me to remove the link.
I think you have to include jQuery to use
responseJSON
.Without jQuery, you could try with responseText and try like
eval("("+req.responseText+")");
UPDATE:Please read the comment regarding
eval
, you can test with eval, but don't use it in working extension.OR
use json_parse : it does not use
eval
Note: I've only tested this in Chrome.
it adds a prototype function to the XMLHttpRequest .. XHR2,
in XHR 1 you probably just need to replace
this.response
withthis.responseText
to return the json in xhr2
EDIT
If you plan to use XHR with
arraybuffer
or other response types then you have to check if the response is astring
.in any case you have to add more checks e.g. if it's not able to parse the json.