本文介绍了从XmlHttpRequest.responseJSON解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我通过XmlHttpRequest得到了JSON。

  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(){ta​​rget.parseJSON(req,url)};
req.send(null);

parseJSON:function(req,url){
if(req.status == 200){
var jsonResponse = req.responseJSON;
var bitlyUrl = jsonResponse.results [url] .shortUrl;
}

我在firefox插件中这样做。当我运行时,我得到的错误jsonResponse是未定义的行 var bitlyUrl = jsonResponse.results [url] .shortUrl; 。我在这里解析JSON时做错了什么?或者这个代码有什么问题?

解决方案

标准的XMLHttpRequest没有 responseJSON 属性,只需 responseText responseXML 。只要有点真正的响应你的请求的一些JSON, responseText 应该包含JSON代码作为文本,所以你所要做的就是解析它: p>

  var jsonResponse = JSON.parse(req.responseText); 

如果您喜欢使用 responseJSON ,但想要比JQuery更轻量级的解决方案,你可能想看看我的JSONHttpRequest。它的工作方式与普通的XMLHttpRequest完全相同,但也提供了 responseJSON 属性。所有你必须改变你的代码将是第一行:

  var req = new JSONHttpRequest(); 

JSONHttpRequest还提供了将JavaScript对象作为JSON轻松发送的功能。更多细节和代码可以在我的博客中找到:。



PS:我知道链接到我自己的博客在这里不会被赞赏,但我认为我的脚本是一个很好的解决方案,所以我贴了它。请留下评论,如果你想我删除这个职位,我会这样做。


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?

解决方案

The standard XMLHttpRequest has no responseJSON property, just responseText and responseXML. 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:

var jsonResponse = JSON.parse(req.responseText);

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 the responseJSON property. All you have to change in your code would be the first line:

var req = new JSONHttpRequest();

JSONHttpRequest also provides functionality to easily send JavaScript objects as JSON. More details and the code can be found in my blog: http://pixelsvsbytes.com/2011/12/teach-your-xmlhttprequest-some-json/.

PS: I'm aware that linking to my own blog won't be appreciated here, but I think my script is a good solution to the problem, so I posted it anyway. Please leave a comment if you want me to remove this post and I'll do so.

这篇关于从XmlHttpRequest.responseJSON解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:07