0

How do I display json data in a list using jQuery?

$(document).ready(function() {


 $.getJSON('example.json', function(data) {

     $.each(data.example, function(key, value) {
               
               var output="";
                output="<li>" + data.example[key].name + "</li>";

                $('#example').append(output);
                
    });

      });
          }

This doesn't seem to display anything.

6
  • 2
    you need: 1) a better formatting of your question; 2) to write the full code you use. Commented Nov 26, 2016 at 16:15
  • just added code Commented Nov 26, 2016 at 16:23
  • sorry the system messed up and wouldn't let me add code Commented Nov 26, 2016 at 16:40
  • I doubt the system messed up. It is used daily by thousands of users. Commented Nov 26, 2016 at 16:48
  • Do you have an HTML element with an id of example? can you show us your HTML? - Also, you do have the file 'example.json' placed on the folder as your HTML, right? (and you are using a server to ask for this file, right?) Commented Nov 26, 2016 at 16:54

3 Answers 3

1
$(document).ready(function() {
    $.getJSON('example.json', function(data) {
        var output = '';
        $.each(data.example, function(key, value) {
            output += '<li>' + value.name + '</li>';
        });
        $('#example').html(output); // <ul id="example"></ul>
    });
});

UPDATE: Working JSON file

{
    "example": [
        {
          "name": "Dr. Sammie Boyer",
          "email": "[email protected]"
        },
        {
          "name": "Eladio Beier",
          "email": "[email protected]"
        },
        {
          "name": "Hilton Borer",
          "email": "[email protected]"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

4 Comments

@Rebekah do you have an HTML element with an id of example? can you show us your HTML?
<ul id="example"> <li id="name" name="name" class="allexamples"></li> </ul>
Your JSON file is the problem. I'll update the answer in a minute to see working JSON data.
I have also removed 'ul' tag, since your #example element is already 'ul'
0

You have a couple of syntax errors. The comma in the each function is in the wrong place and there's no closing ); at the end. The code below should work correctly...

    $(document).ready(function() {


 $.getJSON('example.json', function(data) {

     $.each(data.example, function(key, value) {

               var output="";
                output="<li>" + data.example[key].name + "</li>";

                $('#example').append(output);

    });

      });
          });

6 Comments

instead of using data.example[key].name you could use value.name - and instead of appending in every iteration of the loop, I'd concatenate the string and only append after the loop for improved performance.
how would I do that?
@Rebekah have a look at Wizard's answer
I did and it doesn't seem to work, no idea what I'm doing wrong
do you have an example of the .json file you are processing? Perhaps the error is there?
|
0

This would be my take.

You have multiple syntax errors (which are hard to see because the code is poorly formatted) - formatting the code helps you see those errors.

Note: I avoided making the AJAX call to get the JSON data for the looping parsing example.

$(document).ready(function() {
  var data = {
    "example": [{
      "name": "Dr. Sammie Boyer"
    }, {
      "name": "Eladio Beier",
      "email": "[email protected]"
    }, {
      "name": "Hilton Borer",
      "email": "[email protected]"
    }]
  };

   
  
// we don't have to get the json in an AJAX call for this demo
// $.getJSON('example.json', function(data) {
      var output = "<ul>";
    
      $.each(data.example, function(key, value) {
        output += "<li>" + value.name + "</li>";
      });
      
      output += "</ul>";
      $('#example').append(output);;
// });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="example"></div>

Comments

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.