Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use json object in HTML5

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this article Xiaobian for you to introduce in detail "how to use json object in HTML5", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use json object in HTML5" can help you solve your doubts.

The following shows you how to use json object in HTML5 through the example code. The specific code is as follows:

Json object usage var students = {xiaomin: {name: "xiaoming", grade: 1}, teemo: {name: "teemo", grade: 3}} students = JSON.stringify (students) / / convert JSON to string into variables console.log (students); localStorage.setItem ("students", students); / / store variables in 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 values var jsonStr ='[{"id": "01", "open": false, "pId": "0", "name": "A department"}, {"id": "01", "open": false, "pId": "0", "name": "A department"}, {"id": "011", "open": false, "pId": "01", "name": "A department"} {"id": "03", "open": false, "pId": "0", "name": "A department"}, {"id": "04", "open": false, "pId": "0", "name": "A department"}, {"id": "05", "open": false, "pId": "0", "name": "A department"}, {"id": "06", "open": false, "pId": "0" "name": "Department A"}]' Var jsonObj = JSON.parse (jsonStr); / / converted to json object for (var iBoth IPC interface, which promotes the use of JSON format. In the previous JS before H5, I mentioned the eval method, eval () in JS's handling of JSON in a previous H4, and 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);}

The JSON object is converted to String:

Function myStringify () {var txtJson=document.querySelector ("# txtJsonStr"); var jsonStr2=JSON.stringify (jsonObj); / / the jsonObj here is a JSON object txtJson.value=jsonStr2 } after reading this, the article "how to use json objects in HTML5" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to learn more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report