0

I have written a program to create a json object from the list of values obtained from database and here is how my json looks

{
 1: {
 age: "21",
 name: "arjun",
 gender: "male"
},
 2: {
 age: "30",
 name: "ravi",
 gender: "male"
},
 3: {
 age: "57",
 name: "pushpa",
 gender: "female"
 }
}

Now i want to parse it using jquery and print the result in tabular form in html. I have tried to some extent , but not understanding what to do further, so please need some guidance

My json_parse.js :

$(document).ready(function() {
var url = "http://182.168.1.115:8082/JqueryForm/userdetails_json.jsp"
$.parseJSON(url, function(json) {
    var tr;
    for (var i = 0; i < json.length; i++) {
        tr = $('<tr/>');
        tr.append("<td>" + json[i].name + "</td>");
        tr.append("<td>" + json[i].age + "</td>");
        tr.append("<td>" + json[i].gender + "</td>");
        $("#table").append(tr);
    }
});

});

My list_user.html:

<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">  
</script>
<script src="json_parse.js"></script>
</head>
<body>
    <table name="Table" id="table">

    </table>
</div>
</body>
</html>
5
  • What does your output look like currently? Are you getting any rows at all in your table? Commented Mar 12, 2015 at 16:56
  • Is your json data well-formed? Try to call it directly from a browser address bar. Commented Mar 12, 2015 at 17:08
  • @Daryl just blank html page Commented Mar 12, 2015 at 17:33
  • @collapsar yes sir , i can see my json data very well in my brower Commented Mar 12, 2015 at 17:33
  • Are you receiving any console errors? I would set a break point inside your loop and make sure json is actually formatted the way you think it is. Commented Mar 12, 2015 at 18:03

1 Answer 1

3

Well you've got a couple problems:

  1. You should be using jQuery.getJSON() not jQuery.parseJSON()
  2. You're JSON isn't an array, so length is not defined and you cannot loop through it the way you are.

The easy fix is to re-write your loop using jQuery.each() to enumerate your object:

$.each(json, function(i) {
  var tr = $('<tr/>');
  tr.append("<td>" + json[i].name + "</td>");
  tr.append("<td>" + json[i].age + "</td>");
  tr.append("<td>" + json[i].gender + "</td>");
  $("#table").append(tr);
});

There's a working snippet below to see it in action.

$(document).ready(function() {
  var json = { 1: {age: "21", name: "arjun",gender: "male"}, 2: {age: "30",name: "ravi",gender: "male"}, 3: {age: "57", name: "pushpa", gender: "female"}};
  
  $.each(json, function(i) {
    var tr = $('<tr/>');
    tr.append("<td>" + json[i].name + "</td>");
    tr.append("<td>" + json[i].age + "</td>");
    tr.append("<td>" + json[i].gender + "</td>");
    $("#table").append(tr);
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="table"></table>

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

2 Comments

sir , i had used getJSON before, but i am not seeing any result on my browser when i run my html code
I found your other issue. Please see my updated answer

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.