通过jQUERY将URL中的JSON数据解析为HTML

时间:2018-01-10 22:04:56

标签: jquery html json parsing

我有JSON数据的这个文件:
https://suggest.autocompleteapi.com/ap7d7hg5jezr/drg121016?prefix=pe&size=10

我想在jQUERY中使用非常简单的脚本来解析这些数据,
通过jQUERY到HTML网页到这样的ul风格:

<ul>
<li>K704 Alkoholové zlyhávanie pečene</li>
<li>K741 Skleróza pečene</li>
........
.......
</ul>

非常感谢你的时间

2 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

//if the data is one string from ajax response, 
//you have to parse the string to json object first
//let yourData = JSON.parse(ajax_response);

let yourData = {'suggestion':[
                              {'value':'K704 Alkoholové zlyhávanie pečene'},
                              {'value':'K718 Toxická choroba pečene s inými poruchami pečene'},
                              {'value':'K718 Toxická choroba pečene s inými poruchami pečene'}
                             ]
               }

let ulPlacedAt = $('body'); //you can change it to other object you like
let ulBody = '';
yourData['suggestion'].forEach(function(item) {
    ulBody += '<li>'+item['value'] + '</li>';
  }
)
ulBody = '<ul>' + ulBody + '</ul>';
ulPlacedAt.append(ulBody);

答案 1 :(得分:0)

我会在寻求SO帮助之前先研究一切。这可以通过使用.getJSON

通过Ajax提取数据来实现您的目的

&#13;
&#13;
$.getJSON("https://suggest.autocompleteapi.com/ap7d7hg5jezr/drg121016?prefix=pe&size=10", function(result) {

  var data = result.suggestions;

  for (var i = 0; i < data.length; i++) {
    $('.lists').append('<li>' + data[i].value + '</li>');
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="lists">

</ul>
&#13;
&#13;
&#13;