首页前端开发其他前端知识HTML5中如何使用json对象,代码是什么?

HTML5中如何使用json对象,代码是什么?

时间2024-03-27 08:08:03发布访客分类其他前端知识浏览444
导读:这篇文章给大家分享的是“HTML5中如何使用json对象,代码是什么?”,文中的讲解内容简单清晰,对大家认识和了解都有一定的帮助,对此感兴趣的朋友,接下来就跟随小编一起了解一下“HTML5中如何使用json对象,代码是什么?”吧。...
这篇文章给大家分享的是“HTML5中如何使用json对象,代码是什么?”,文中的讲解内容简单清晰,对大家认识和了解都有一定的帮助,对此感兴趣的朋友,接下来就跟随小编一起了解一下“HTML5中如何使用json对象,代码是什么?”吧。

这篇文章主要介绍了HTML5中使用json对象的实例代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

下面通过实例代码给大家介绍HTML5中使用json对象的方法,具体代码如下所示:

!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对象用法/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>
    
        p  class="prism-player" id="J_prismPlayer" style="position: absolute">
    /p>
    
        script>

        var students = {

          xiaomin: {

            name: "xiaoming",
            grade: 1
          }
,
          teemo: {

            name: "teemo",
            grade: 3
          }

        }
    
        students = JSON.stringify(students);
     //将JSON转为字符串存到变量里
        console.log(students);
    
        localStorage.setItem("students",students);
    //将变量存到localStorage里
        var newStudents = localStorage.getItem("students");
    
        newStudents = JSON.parse(students);
     //转为JSON
        console.log(newStudents);
     // 打印出原先对象
    //  alert(newStudents.length);
    
        alert(newStudents.xiaomin.name);

         //json数组类型字符串取值
        var jsonStr = '[{
"id":"01","open":false,"pId":"0","name":"A部门"}
,{
"id":"01","open":false,"pId":"0","name":"A部门"}
,{
"id":"011","open":false,"pId":"01","name":"A部门"}
,{
"id":"03","open":false,"pId":"0","name":"A部门"}
,{
"id":"04","open":false,"pId":"0","name":"A部门"}
, {
"id":"05","open":false,"pId":"0","name":"A部门"}
, {
"id":"06","open":false,"pId":"0","name":"A部门"}
    ]';
    
        var jsonObj =  JSON.parse(jsonStr);
    //转换为json对象
        for(var i=0;
    ijsonObj.length;
i++){
    
                alert(jsonObj[i].id);
  //取json中的值
        }
    
        console.log(jsonObj)
        var jsonStr1 = JSON.stringify(jsonObj)
        console.log(jsonStr1+"jsonStr1")
        /script>
    
    /body>
    
/html>
    

补充:下面看下Html5中JSON对象与String的互相转换

面对现在移动端的迅速发展,提供数据的方式不在是以前的PC--> PC界面了,这促使了JSON格式的使用,在H5以前的JS中,我在前面的一个H4中JS对JSON中的处理提到了eval方法,eval() ,在H5中JSON与String的转换如下:

String转换为JSON对象:

var jsonObj;

function myParse(){
    
     var jsonStr=document.querySelector("#txtJsonStr").value;
    
     jsonObj=JSON.parse(jsonStr);

}

JSON对象转换为String:

function myStringify(){
    
     var txtJson=document.querySelector("#txtJsonStr");
    
     var jsonStr2=JSON.stringify(jsonObj);
     //此处的jsonObj 是一个 JSON对象
     xtJson.value=jsonStr2;

}
    



到此这篇关于“HTML5中如何使用json对象,代码是什么?”的文章就介绍到这了,感谢各位的阅读,更多相关HTML5中如何使用json对象,代码是什么?内容,欢迎关注网络资讯频道,小编将为大家输出更多高质量的实用文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

html5json对象jsonstring转换

若转载请注明出处: HTML5中如何使用json对象,代码是什么?
本文地址: https://pptw.com/jishu/654089.html
用canvas怎么制作下雪效果,代码是什么 html5里面figcaption与figure的使用方法是什么

游客 回复需填写必要信息