Я пишу API, который обращается к библейским проходам от http://labs.bible.org/, и ответы 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 – 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’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
Большинство языков имеют JSON парсер, который поможет вам получить доступ к элементам JSON. На каком языке вы используете? Для java есть GSON, для Python и JavaScript есть встроенная поддержка json, для Objective-C - JSONKit, для Perl - пакет JSON ... и т. Д. – PaulProgrammer
Является ли этот API написанным на определенном языке программирования? – summea
Я пишу это в JavaScript. Я знаю, что у него есть метод JSON.parse(), но я всегда видел «ответ» в верхней части JSON, поэтому я хотел бы получить доступ к различной информации, например (data responseText) «data.response.bookname», и если было более одного результата, тогда я бы установил цикл для их увеличения. Я просто не понимаю, как я это сделаю. –