javascript - Appending json file in html -
i have json file ticket.json
"{\"item1\":[{\"id\":2,\"title\":\"support\",\"totalunresolveditems\":14},{\"id\":8,\"title\":\"helpdesk\",\"totalunresolveditems\":1},{\"id\":7,\"title\":\"hostmaster\",\"totalunresolveditems\":1},{\"id\":4,\"title\":\"salg\",\"totalunresolveditems\":1}],\"item2\":[{\"id\":8,\"fullname\":\"person 1\",\"totalunresolveditems\":5},{\"id\":3,\"fullname\":\"person 2\",\"totalunresolveditems\":4},{\"id\":11,\"fullname\":\"person 3\",\"totalunresolveditems\":3},{\"id\":14,\"fullname\":\"person 4\",\"totalunresolveditems\":1},{\"id\":12,\"fullname\":\"person 5\",\"totalunresolveditems\":1},{\"id\":7,\"fullname\":\"person 7\",\"totalunresolveditems\":1},{\"id\":4,\"fullname\":\"person 8\",\"totalunresolveditems\":1}]}"
and trying append index.html. far have tried below codes , able se json file in console.log. error xml tolkningsfail not understand why. me out solve problem. thank you.
<html> <head> <meta charset=utf-8> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"> </script> <script> $(document).ready(function() { var dashboard = []; $.getjson('ticket.json', function(data) { data = json.parse(data); console.log(data); var keys = object.keys(data); (var = 0; < keys.length; i++) { if (data.hasownproperty(keys[i])) { $.each(data[keys[i]], function (index, item) { var tblrow = "<tr>" + "<td>" + item.id + "</td>" + "<td>" + item.title + "</td>" +"<td>" + item.totalunresolveditems + "</td>" + "</tr>" var tblrow2 = "<tr>" + "<td>" + item.id + "</td>" + "<td>" + item.fullname + "</td>" +"<td>" + item.totalunresolveditems + "</td>" + "</tr>" $(tblrow).appendto("#userdata tbody"); $(tblrow2).appendto("#userdata2 tbody"); }); } } }); }); </script> </head> <body> <div class="wrapper"> <div class="profile"> <table id= "userdata" border="2"> <thead> <th>id</th> <th>title</th> <th>totalunresolveditems</th> </thead> <tbody> </tbody> </table> <table id= "userdata2" border="2"> <thead> <th>id</th> <th>fullname</th> <th>totalunresolveditems</th> </thead> <tbody> </tbody> </table> </div> </div> </body> </html>
$(document).ready(function() { var elmjson = "{\"item1\":[{\"id\":2,\"title\":\"support\",\"totalunresolveditems\":14},{\"id\":8,\"title\":\"helpdesk\",\"totalunresolveditems\":1},{\"id\":7,\"title\":\"hostmaster\",\"totalunresolveditems\":1},{\"id\":4,\"title\":\"salg\",\"totalunresolveditems\":1}],\"item2\":[{\"id\":8,\"fullname\":\"person 1\",\"totalunresolveditems\":5},{\"id\":3,\"fullname\":\"person 2\",\"totalunresolveditems\":4},{\"id\":11,\"fullname\":\"person 3\",\"totalunresolveditems\":3},{\"id\":14,\"fullname\":\"person 4\",\"totalunresolveditems\":1},{\"id\":12,\"fullname\":\"person 5\",\"totalunresolveditems\":1},{\"id\":7,\"fullname\":\"person 7\",\"totalunresolveditems\":1},{\"id\":4,\"fullname\":\"person 8\",\"totalunresolveditems\":1}]}" var dashboard = []; dashboard.push(json.parse(elmjson)); //console.log(dashboard); dashboard.foreach(function(value){ //console.log(value) (var key in value) { if (value.hasownproperty(key)) { //console.log(value[key]); value[key].foreach(function(val){ var tblrow = "<tr>" + "<td>" + val.id + "</td>" + "<td>" + val.fullname + "</td>" +"<td>" + val.totalunresolveditems + "</td>" + "</tr>" var tblrow2 = "<tr>" + "<td>" + val.id + "</td>" + "<td>" + val.title + "</td>" +"<td>" + val.totalunresolveditems + "</td>" + "</tr>" $(tblrow).appendto("#userdata tbody"); $(tblrow2).appendto("#userdata2 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>id</th> <th>title</th> <th>totalunresolveditems</th> </thead> <tbody> </tbody> </table> <table id= "userdata2" border="2"> <thead> <th>id</th> <th>fullname</th> <th>totalunresolveditems</th> </thead> <tbody> </tbody> </table> </div> </div>
once data, can following:
$(document).ready(function() { var elmjson = "{\"item1\":[{\"id\":2,\"title\":\"support\",\"totalunresolveditems\":14},{\"id\":8,\"title\":\"helpdesk\",\"totalunresolveditems\":1},{\"id\":7,\"title\":\"hostmaster\",\"totalunresolveditems\":1},{\"id\":4,\"title\":\"salg\",\"totalunresolveditems\":1}],\"item2\":[{\"id\":8,\"fullname\":\"andré lund\",\"totalunresolveditems\":5},{\"id\":3,\"fullname\":\"arne hundewadt\",\"totalunresolveditems\":4},{\"id\":11,\"fullname\":\"erik nymand\",\"totalunresolveditems\":3},{\"id\":14,\"fullname\":\"michael povlsen\",\"totalunresolveditems\":1},{\"id\":12,\"fullname\":\"rene nicolaj buch\",\"totalunresolveditems\":1},{\"id\":7,\"fullname\":\"bjørn hansen\",\"totalunresolveditems\":1},{\"id\":4,\"fullname\":\"jakob wensien-jegsen\",\"totalunresolveditems\":1}]}" var dashboard = []; dashboard.push(json.parse(elmjson)); dashboard.foreach(function(value, index){; (var key in value) { if (value.hasownproperty(key)) { if(key==='item1'){ value[key].foreach(function(val){ var tblrow = "<tr>" + "<td>" + val.id + "</td>" + "<td>" + val.title + "</td>" +"<td>" + val.totalunresolveditems + "</td>" + "</tr>" $(tblrow).appendto("#userdata tbody"); }) } if(key==='item2'){ value[key].foreach(function(val){ var tblrow = "<tr>" + "<td>" + val.id + "</td>" + "<td>" + val.fullname + "</td>" +"<td>" + val.totalunresolveditems + "</td>" + "</tr>" $(tblrow).appendto("#userdata2 tbody"); }) } } } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="profile"> <strong>item1:</strong> <table id= "userdata" border="2"> <thead> <th>id</th> <th>title</th> <th>totalunresolveditems</th> </thead> <tbody> </tbody> </table> <strong>item2:</strong> <table id= "userdata2" border="2"> <thead> <th>id</th> <th>fullname</th> <th>totalunresolveditems</th> </thead> <tbody> </tbody> </table> </div> </div>
Comments
Post a Comment