简体   繁体   中英

JSON Data and Manipulating the Content

I am trying to write out the data from this JSON url into li's.

The JSON link is https://www.inquicker.com/facility/americas-family-doctors.json

    <!DOCTYPE html>
    <html>
    <head>
    <title>JQuery (cross-domain) JSONP</title>
    <script  type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">    </script>
    <script>
$(document).ready(function(){
    $.getJSON('https://www.inquicker.com/facility/americas-family-doctors.json', 
        function(data){ 
        alert(data.facility);
        $.each(data.schedules, function(i, name){
            $('#names').append('<li>' + name.available_times[0] + '</li>');
        });
    });
});
    </script>
    </head>
    <body>
    <ul id="names"></ul>
    </body>
    </html>

You can add a test to check if the length of available_times array is > 0 before accessing the first cell.

And then, you can access to when or url properties :

  • available_times[0].when
  • available_times[0].url

Edit : save name.available_times[0] in a temp var and write temp.when or temp.url .

In the JSON link, some schedule s have empty available_times arrays.

And available_times[0] is an object that contains the properties when and url , so you'll have to write name.available_times[0].url

I have created a JSFidle check this . You to check the data is available in available_times then proceed. if (name.available_times.length){....... ..... ....

$(document).ready(function(){
$.getJSON('https://www.inquicker.com/facility/americas-family-doctors.json',
    function(data){

    $.each(data.schedules, function(i, name){
        times=''
            if (name.available_times.length){
                times='<ul>'
                times+='<li><a href="'+name.available_times[0].url+'">'+name.available_times[0].when+'</a></li>'
                times+='</ul>'
            }
            else{
               times='<ul><li>No Time Available</li></ul>'                    
            }
        $('#names').append('<li>' + (name.name) + times + '</li>');
    });
});

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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