Converting JSON array result from responseText to structured headings and paragraphs -
using xmlhttprequest , json open array:
var xmlhttp=new xmlhttprequest(); xmlhttp.open("get","http://serv.json",false); xmlhttp.send(); result=xmlhttp.responsetext; document.getelementbyid("sec1").innerhtml=xmlhttp.responsetext;
results come through this:
[ { "name": "", "description": "" }, { "name": "" , "description": "" }]
need structure results headings names ,
paragraphs description. tried several loops none seem working. correct loop use?
incorrect loop: (not sure place createelement , appendchild
for (i=o; i> fruit.length;i++){ document.getelementbyid("sec1").innerhtml=fruit[0].name + "" + fruit[0].description:; }
try along lines of following:
<html> <body> <div id="sections"> </div> <script> var request = new requestrequest(); request.open("get","http://serv.json",false); request.send(null); if (request.status === 200) { var response = json.parse(request.responsetext), sections = document.getelementbyid("sections"); (var = 0; < response.length; ++i) { var section = document.createelement('div'), nameelement = document.createelement('h2'), descriptionelement = document.createelement('p'); nameelement.innerhtml = response[i].name; descriptionelement = response[i].description; section.appendchild(nameelement); section.appendchild(descriptionelement); sections.appendchild(section); } } </script> </body> </html>
Comments
Post a Comment