2013-04-09 74 views
1

我正在编写一个访问来自http://labs.bible.org/的圣经段落的API,并且JSON响应没有“响应”头或任何层次结构名称而回来。 Firebug显示我的GET请求回到200状态,但响应选项卡始终为空。如果我直接在浏览器中输入url,我会得到我想要的结果,但我不知道如何处理这样的JSON。例如:http://labs.bible.org/api/?passage=luke+9&formatting=full&type=json无标题JSON响应

这就是JSON的样子。

[ 
    { 
     "bookname": "Luke", 
     "chapter": "9", 
     "verse": "1", 
     "text": "<t /><p class=\"bodytext\">After<n id=\"1\" /> Jesus<n id=\"2\" /> called<n id=\"3\" /> the twelve<n id=\"4\" /> together, he gave them power and authority over all demons and to cure<n id=\"5\" /> diseases,", 
     "title": "The Sending of the Twelve Apostles" 
    }, 
    { 
     "bookname": "Luke", 
     "chapter": "9", 
     "verse": "2", 
     "text": "and he sent<n id=\"1\" /> them out to proclaim<n id=\"2\" /> the kingdom of God<n id=\"3\" /> and to heal the sick.<n id=\"4\" />" 
    }, 
    { 
     "bookname": "Luke", 
     "chapter": "9", 
     "verse": "3", 
     "text": "He<n id=\"1\" /> said to them, “Take nothing for your<n id=\"2\" /> journey &#8211; no staff,<n id=\"3\" /> no bag,<n id=\"4\" /> no bread, no money, and do not take an extra tunic.<n id=\"5\" />" 
    }, 
    { 
     "bookname": "Luke", 
     "chapter": "9", 
     "verse": "4", 
     "text": "Whatever<n id=\"1\" /> house you enter, stay there<n id=\"2\" /> until you leave the area.<n id=\"3\" />" 
    }, 
    { 
     "bookname": "Luke", 
     "chapter": "9", 
     "verse": "5", 
     "text": "Wherever<n id=\"1\" /> they do not receive you,<n id=\"2\" /> as you leave that town,<n id=\"3\" /> shake the dust off<n id=\"4\" /> your feet as a testimony against them.”" 
    }, 
    { 
     "bookname": "Luke", 
     "chapter": "9", 
     "verse": "6", 
     "text": "Then<n id=\"1\" /> they departed and went throughout<n id=\"2\" /> the villages, proclaiming the good news<n id=\"3\" /> and healing people everywhere.</p>" 
    }, 
    { 
     "bookname": "Luke", 
     "chapter": "9", 
     "verse": "7", 
     "text": "<t /><p class=\"bodytext\">Now Herod<n id=\"1\" /> the tetrarch<n id=\"2\" /> heard about everything that was happening, and he was thoroughly perplexed,<n id=\"3\" /> because some people were saying that John<n id=\"4\" /> had been raised from the dead,", 
     "title": "Herod&#8217;s Confusion about Jesus" 
    }, 
    (...) 
] 

那么如何编写访问和解析JSON的代码,以及如何循环遍历所有结果呢?

这是我用来获取和解析JSON功能:

function callApi(argument, callBack){ 

     var requestUrl = apiUrl+argument+type; 

     try{ 
      var request = new XMLHttpRequest(); 

      request.addEventListener("readystatechange", 
      function() {callBack(request);}, false); 

      request.open("GET", requestUrl, true); 
      request.setRequestHeader("Accept", "application/json; charset=utf-8"); 
      request.send(); 
     }//end try 
     catch(exception){ 
      alert("Request Failed"); 
     }//end catch 
    }//end function callApi 

function parseData(request){ 

    if(request.readyState==4 && request.status==200){ 
     var data = JSON.parse(request.responseText); 
     displayNames(data); 
    }//end if 
}// end function parseData 
+1

大多数语言都有一个JSON解析器,这将有助于您访问JSON元素。你在用什么语言?对于java,有GSON,对于Python和JavaScript,有内置的json支持,Objective-C有JSONKit,Perl有JSON包等等。 – PaulProgrammer 2013-04-09 17:19:49

+0

这个API是用某种编程语言编写的吗? – summea 2013-04-09 17:20:00

+1

我正在用JavaScript编写它。我知道它有一个JSON.parse()方法,但我总是在JSON的顶部看到“响应”,所以我将访问不同的信息,如(数据是responseText)“data.response.bookname”如果有多个结果,那么我会设置一个循环来增加它们。我只是不明白我会如何做到这一点。 – 2013-04-09 17:27:21

回答

0

如果你使用的是不同的JSON的URL这样的尝试:(去除&formatting=full,并在年底加入&callback=?http://labs.bible.org/api/?passage=luke+9:3&type=json&callback=?

如果你没有格式化标签(所有<HTML>标签)似乎解析对我来说,至少在此的jsfiddle例如响应: http://jsfiddle.net/L8Fed/2/

更新:下面是如何的jQuery在这里可以用来从您的网址抓取JSON的例子:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script> 
$(function() { 
    var json_url = "http://labs.bible.org/api/?passage=luke+9:3&type=json&callback=?"; 

    $.getJSON(json_url, function(json_response) { 
     for(var i = 0; i < json_response.length; i++) { 
      alert(json_response[i].text); 
     } 
    }); 
}); 
</script> 

基本上,这个代码:

  1. 包括jQuery
  2. 将JavaScript放入ready函数more details here .. 。)
  3. 确保调用包含&callback=?for this reason
  4. 调用jQuery的$.getJSON()功能的API URL (使用您的API URL和回来命名json_response响应)
  5. 循环访问json_response,并允许您获取所需的任何值。 (你可能会想取出alert()函数在某一点...;)
+1

您发布的代码实质上就是我想要做的。当然不是排队,而是相同的概念。我想真正的问题是对我的HTTP GET的响应返回为空。我将发布我的函数来检索和解析数据。 – 2013-04-09 18:31:15

+0

@JaylinFrederick感谢您发布这些功能;只是想知道,但是......是否可以(在您的项目的参数范围内)使用jQuery而不是手动编写“GET JSON”部分? – summea 2013-04-09 19:37:18

+1

是的,这是完全可能的,但问题是我不熟悉jQuery。我仍在试图了解它。 – 2013-04-09 19:46:14