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 的样子。

[
    {
      "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
4

1 回答 1

0

如果您尝试使用像这样的不同 JSON URL 会怎样:(在末尾http://labs.bible.org/api/?passage=luke+9:3&type=json&callback=? 删除&formatting=full和添加。)&callback=?

如果你得到没有格式化标签(所有<HTML>标签)的响应,它似乎为我解析,至少在 jsFiddle 上的这个例子中:http: //jsfiddle.net/L8Fed/2/

更新:这是一个如何在这里使用 jQuery 从您的 URL 中获取 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函数中更多细节在这里......)
  3. 确保调用最后包含的 API URL &callback=?出于这个原因
  4. 调用 jQuery 的$.getJSON()函数(使用您的 API URL 并返回命名的响应json_response
  5. 循环json_response并允许您获得所需的任何值。 (而且您可能希望alert()在某个时候取消该功能......;)
于 2013-04-09T18:03:18.467 回答