1

I have this file wiht JSON data:
https://suggest.autocompleteapi.com/ap7d7hg5jezr/drg121016?prefix=pe&size=10

and I want very simple script in jQUERY to parse this data,
via jQUERY to HTML webpage to ul style like this:

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

Thank you very much for your time

3
  • Have you tried anything yet? Commented Jan 10, 2018 at 22:15
  • Yes, script : w3schools.com/jquery/ajax_getjson.asp, I change URL but dont work Commented Jan 10, 2018 at 22:16
  • @Peter The script there is for appending plain text to to a DIV. Did you change it to add <li> element to your <ul>? Show your actual code. Commented Jan 10, 2018 at 22:42

2 Answers 2

1

I'd say research everything prior to seeking help on SO. This does what you want by pulling the data via Ajax using .getJSON

$.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>');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="lists">

</ul>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanx, it works, you save me a lot of time, I am new in jQUERY
@Peter What's the difference between this and what you originally tried? Except for using a for loop instead of $.each, it seems identical to the w3schools page you mentioned in the comment.
0

You can try below:

//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);

2 Comments

Thanx it works, but I have esternal URL file with JSON: suggest.autocompleteapi.com/ap7d7hg5jezr/…, put this file in "let yourData"?
try this: $.ajax({ url: 'your_url', type: 'POST', data: post_params, async: true, cache: false, success: function (result) { var yourData = JSON.parse(result);}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.