简体   繁体   English

JavaScript:如何在AJAX调用中使用jQuery遍历JSON对象

[英]JavaScript: How to Loop through JSON objects with jQuery in an AJAX call

I am having difficulty trying to learn this concept. 我在尝试学习这个概念时遇到了困难。

I have a JSON file with a list of hospitals, their address, hours and phone. 我有一个JSON文件,其中包含医院,其地址,时间和电话的列表。

I want to make an AJAX call from JSON data and list the data onto the screen. 我想从JSON数据进行AJAX调用,然后将数据列出到屏幕上。

I can only get the first hospital to list, when I try to list the address only the address lists. 当我尝试仅列出地址列表时,我只能列出第一家医院。

How can I write this using a loop that will list the objects and their properties? 如何使用列出对象及其属性的循环编写此代码?

Can someone explain this to me, please? 有人可以向我解释一下吗?

Please help -- thank you in advance. 请帮助-预先谢谢您。

JSON -- JSON-

https://api.myjson.com/bins/b29r7 https://api.myjson.com/bins/b29r7

JS -- JS-

var url = 'https://api.myjson.com/bins/b29r7';

$.ajax({
    url: url,
    method: 'GET',
}).done(function(result){
    var data = result.surrey;
    for(var i=0; i<data.length; i++){
        $('#data').each(function(index, ele){
            $(ele).html(data[index].hospital);
        });
    }
}).fail(function(err){
    throw err;
});

HTML -- HTML-

<p id="data"></p>

Here's a working example: 这是一个工作示例:

 var url = 'https://api.myjson.com/bins/b29r7'; $.ajax({ url: url, method: 'GET', }).done(function(result) { // JSON data array var data = result.surrey; // get DOM node to be parent of child list nodes var $data = $('#data'); // iterate through each object in JSON array data.forEach(function(item) { // create an unordered list node var $ul = $('<ul></ul>'); // iterate through all the properties of current JSON object for (var field in item) { // append list item node to list node $ul.append(`<li>${field}: ${item[field]}</li>`); } // append list node to parent node $data.append($ul); }); }).fail(function(error) { console.error(error); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="data"></div> 

JSFiddle Demo: https://jsfiddle.net/L6j8n17j/4/ JSFiddle演示: https ://jsfiddle.net/L6j8n17j/4/

UPDATE : This is more clear version. 更新:这是更清晰的版本。 You can print your values in your html like this. 您可以像这样在html中打印值。

 $.ajax({ type: 'ajax', url: 'https://api.myjson.com/bins/b29r7', async: false, dataType: 'json', success: function (data) { data = data.surrey; var html = ''; var i; for (i = 0; i < data.length; i++) { html+='<span>'+data[i].hospital+'</span>'; html+='<span>'+data[i].address+'</span>'; //so on } $('#data').html(html); }, error: function () { alert('Could not get Data'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='data'> </div> 

This should bring your values on the console and then you can use html function at the same time in the loop to display the data. 这应该在控制台上显示您的值,然后您可以在循环中同时使用html函数来显示数据。 Thanks! 谢谢!

as per requirement please try below code 根据要求,请尝试以下代码

 var url = 'https://api.myjson.com/bins/b29r7'; $.ajax({ url: url, method: 'GET', }).done(function(result){ var data = result.surrey; var i = 0; var hosData = "<table border='1'>"; hosData += "<tr>"; hosData += "<th>"; hosData += 'hospital'; hosData += "</th>"; hosData += "<th>"; hosData += 'address'; hosData += "</th>"; hosData += "<th>"; hosData += 'hours'; hosData += "</th>"; hosData += "<th>"; hosData += 'phone'; hosData += "</th>"; hosData += "</tr>"; for(i=0;i<data.length;i++) { hosData += "<tr>"; hosData += "<td>"; hosData += data[i].hospital; hosData += "</td>"; hosData += "<td>"; hosData += data[i].address; hosData += "</td>"; hosData += "<td>"; hosData += data[i].hours; hosData += "</td>"; hosData += "<td>"; hosData += data[i].phone; hosData += "</td>"; hosData += "</tr>"; } hosData += "</table>"; $("#data").html(hosData); }).fail(function(err){ throw err; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="data"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM