The following is an introduction to how to use json objects in HTML5 through example code. The specific code is as follows:
<!DOCTYPE html><html> <head> <meta charset=UTF-8> <meta name=viewport content=width=device-width, height=device-height, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no/> <title>json object usage</title> <link rel=stylesheet href=//g.alicdn.com/de/prismplayer/2.6.0/skins/default/aliplayer-min.css /> <script type=text/javascript src=//g.alicdn.com/de/prismplayer/ 2.6.0/aliplayer-min.js></script> </head> <body> <div class=prism-player id=J_prismPlayer style=position: absolute></div> <script> var students = { xiaomin: { name: xiaoming, grade: 1 }, teemo: { name: teemo, grade: 3 } } students = JSON.stringify(students); //Convert JSON to a string and store it in a variable console.log(students); localStorage.setItem(students,students);//Save variables into localStorage var newStudents = localStorage.getItem(students); newStudents = JSON.parse(students); //Convert to JSON console.log(newStudents); // Print out the original object // alert(newStudents.length); alert(newStudents.xiaomin.name); //json array type string value var jsonStr = '[{id:01,open:false,pId:0,name:Department A},{id:01,open:false,pId:0,name:Department A},{id:011,open:fals e,pId:01,name:Department A},{id:03,open:false,pId:0,name:Department A},{id:04,open:false,pId:0,name:Department A}, {id:05,open:false,pId:0,name:Department A}, {id:06,open:false,pId:0,name:Department A}]'; var jsonObj = JSON.parse(jsonStr); //Convert to json object for(var i=0;i<jsonObj.length;i++){ alert(jsonObj[i].id); //Get the value in json} console.log(jsonObj) var jsonStr1 = JSON.stringify(jsonObj) console.log(jsonStr1+jsonStr1) </script> </body></html> Supplement: Let’s take a look at the mutual conversion between JSON objects and String in Html5Faced with the rapid development of mobile terminals, the way to provide data is no longer the previous PC-->PC interface, which has prompted the use of JSON format. In JS before H5, I used JS to JSON in the previous H4 The processing mentioned the eval method, eval(). The conversion between JSON and String in H5 is as follows:
Convert String to JSON object:
var jsonObj; function myParse(){ var jsonStr=document.querySelector(#txtJsonStr).value; jsonObj=JSON.parse(jsonStr); }Convert JSON object to String:
function myStringify(){ var txtJson=document.querySelector(#txtJsonStr); var jsonStr2=JSON.stringify(jsonObj); //jsonObj here is a JSON object txtJson.value=jsonStr2; } SummarizeThe above is the example code for using json objects in HTML5 introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for your support of the VeVb martial arts website!