App下載

在HTML5中怎么使用json對(duì)象?案例分享!

猿友 2021-07-16 13:52:55 瀏覽數(shù) (2674)
反饋

今天我們來(lái)講講有關(guān)于:“在HTML5中怎么使用json對(duì)象?”這個(gè)問(wèn)題吧!下面是小編整理的相關(guān)內(nèi)容,希望對(duì)大家有所幫助!

下面通過(guò)實(shí)例代碼給大家介紹HTML5中使用json對(duì)象的方法,具體代碼如下所示:

<!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對(duì)象用法</title>
     <link rel="stylesheet"  />
     <script type="text/javascript" src="http://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); //將JSON轉(zhuǎn)為字符串存到變量里
        console.log(students);
        localStorage.setItem("students",students);//將變量存到localStorage里
        var newStudents = localStorage.getItem("students");
        newStudents = JSON.parse(students); //轉(zhuǎn)為JSON
        console.log(newStudents); // 打印出原先對(duì)象
    //  alert(newStudents.length);
        alert(newStudents.xiaomin.name);
         //json數(shù)組類型字符串取值
        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);//轉(zhuǎn)換為json對(duì)象
        for(var i=0;i<jsonObj.length;i++){
                alert(jsonObj[i].id);  //取json中的值
        }
        console.log(jsonObj)
        var jsonStr1 = JSON.stringify(jsonObj)
        console.log(jsonStr1+"jsonStr1")
        </script>
    </body>
</html>

補(bǔ)充:下面看下Html5中JSON對(duì)象與String的互相轉(zhuǎn)換

面對(duì)現(xiàn)在移動(dòng)端的迅速發(fā)展,提供數(shù)據(jù)的方式不在是以前的PC-->PC界面了,這促使了JSON格式的使用,在H5以前的JS中,我在前面的一個(gè)H4中JS對(duì)JSON中的處理提到了eval方法,?eval() ?  ,在H5中JSON與String的轉(zhuǎn)換如下:

            String轉(zhuǎn)換為JSON對(duì)象:       

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

          JSON對(duì)象轉(zhuǎn)換為String:             

     function myStringify(){
                          var txtJson=document.querySelector("#txtJsonStr");
                           var jsonStr2=JSON.stringify(jsonObj); //此處的jsonObj 是一個(gè) JSON對(duì)象
                                   txtJson.value=jsonStr2; }

總結(jié)

相信通過(guò)這篇文章大家對(duì)于:“在HTML5中怎么使用json對(duì)象?”這個(gè)問(wèn)題應(yīng)該有所了解了吧!當(dāng)然更多有關(guān)于html5這方面的相關(guān)知識(shí)和內(nèi)容我們都可以在W3Cschool中進(jìn)行學(xué)習(xí)。 


0 人點(diǎn)贊