0

I am trying to readed json file in jquery with javascript. What is my wrong with this code

$(function() {
  var new4 = [];
  $.getJSON('new4.json', function(data) {
    $.each(data.new4, function(i, f) {
        var tblRow = "<tr>" + "<td>" + f.FirstName + "</td>" +
          "<td>" + f.LastName + "</td>" + "<td>" + f.EmailAddress + "</td>" + "<td>" + f.City + "</td>" + "</tr>"
        $(tblRow).appendTo("#userdata tbody");
      }
      ss);

  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>


</head>

<body>

  <div class="wrapper">
    <div class="profile">
      <table id="userdata" border="2">
        <thead>
          <th>FirstName</th>
          <th>LastName</th>
          <th>EmailAddress</th>
          <th>City</th>
        </thead>
        <tbody>

        </tbody>
      </table>

    </div>
  </div>

</body>

</html>

And my json codes

{
   "person": [
       {
           "firstName": "Clark",
           "lastName": "Kent",
           "Email Address": "Reporter",
           "City": 20
       },
       {
           "firstName": "Bruce",
           "lastName": "Wayne",
           "Email Address": "Playboy",
           "City": 30
       },
       {
           "firstName": "Peter",
           "lastName": "Parker",
           "Email Address": "Photographer",
           "City": 40
       }
   ]
}

For source, I download a jquery file fromwebsite to my folder.

I added this picture because of source is very important about json and I want to be sure my way is ture for create a html.

When I start html there is not data as shown in picture Output of html

2
  • What error message you getting this time? Commented Mar 17, 2017 at 7:43
  • Actually nothing. Html opens but there is not a array or someting which inculude json file. When I start, just shown html file without information that given with json. Commented Mar 17, 2017 at 7:44

2 Answers 2

2

Assuming that your AJAX request is working, there are several syntax problems in your code.

  • You have a random ss before the $.each() closing brace
  • You're looping over data.new4 which doesn't exist. You need to loop over the data.person array
  • The property names you're using do not match those in the object. Note that JS is case sensitive.
  • You need to use bracket notation to access the 'Email Address' property as it contains a space - or better still change it to match the format of the other properties, ie. emailAddress. The same should be done for City too.
  • While not an error, you don't need to append string literals together, just put them in the same string.

With those fixes in place your code should work:

var data = {
    "person": [{
        "firstName": "Clark",
        "lastName": "Kent",
        "Email Address": "Reporter",
        "City": 20
    }, {
        "firstName": "Bruce",
        "lastName": "Wayne",
        "Email Address": "Playboy",
        "City": 30
    }, {
        "firstName": "Peter",
        "lastName": "Parker",
        "Email Address": "Photographer",
        "City": 40
    }]
}

//$.getJSON('new4.json', function(data) {
    $.each(data.person, function(i, person) {
        var tblRow = "<tr><td>" + person.firstName + "</td><td>" + person.lastName + "</td><td>" + person['Email Address'] + "</td><td>" + person.City + "</td></tr>"
        $(tblRow).appendTo("#userdata tbody");
    });
//});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
    <div class="profile">
        <table id="userdata" border="2">
            <thead>
                <th>FirstName</th>
                <th>LastName</th>
                <th>EmailAddress</th>
                <th>City</th>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>

Also note that all of the above errors can be found by checking the console in your browser. This can generally be accessed by pressing F12

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

2 Comments

Thank you for Your interest. That way works in same html but I will take json file from folder as this json folder will update always. If I take json file from a folder how can I update my codes. I wrote new4.json because this is which json file in folder. According to your codes How can I get json codes from out side. And I tried that codes here. jsfiddle.net/b5fr55qy but when I take this code on notepad++ they don't work.
This is a simplified version of your code. I only included the data in the answer as I can't make an AJAX request here. Just uncomment the $.getJSON lines.
1

If i understand you correctly following code will solve your issue.

Please change your .each function with this

$.each(data.person, function(i, f) {
  var tblRow = "<tr>" + "<td>" + f.firstName + "</td>" +
    "<td>" + f.lastName + "</td>" + "<td>" + f["Email Address"] + "</td>" + "<td>" + f.City + "</td>" + "</tr>"
  $(tblRow).appendTo('body');
});

var data = {
   "person": [
       {
           "firstName": "Clark",
           "lastName": "Kent",
           "Email Address": "Reporter",
           "City": 20
       },
       {
           "firstName": "Bruce",
           "lastName": "Wayne",
           "Email Address": "Playboy",
           "City": 30
       },
       {
           "firstName": "Peter",
           "lastName": "Parker",
           "Email Address": "Photographer",
           "City": 40
       }
   ]
}

$.each(data.person, function(i, f) {
  var tblRow = "<tr>" + "<td>" + f.firstName + "</td>" +
    "<td>" + f.lastName + "</td>" + "<td>" + f["Email Address"] + "</td>" + "<td>" + f.City + "</td>" + "</tr>"
  $(tblRow).appendTo('body');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body></body>

1 Comment

Note that f.EmailAddress will result in undefined.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.