初识 JSON与JSON 的 3 种形式

发布时间 2023-03-22 21:17:14作者: 95232

初识 JSON

    1.JSON是什么

      Ajax 发送和接收书数据的一种格式

      XML

      username=alex&age=18

      JSON

        Json 全称是JavaScript Object Notation

      2.为什么需要JSON

      JSON有3中形式,每种形式的写法都和JS中的数据类型很像,可以很轻松的和JS中的数据类型互相转换

        JS->JSON->PHP/Java 

        PHP/Java->JSON->JS

      

 

 

 

JSON 的 3 种形式

     1.简单值形式

          

 

       

 

 

     JSON的简单形式就对应着JS中的基础数据类型

     数字、字符串、布尔值、null

       注意:

          JSON中没有undefined 值
          JSON中的字符串必须使用双引号

          JSON中是不能注释的

    2. 对象形式

        JSON的对象形式就对应着JS中的对象 

        

 

 

              注意事项

              JSON中对象的属性名必须使用双引号,属性值如果是字符串也必须使用双引号  

               JSON 中只要涉及到字符串,就必须使用双引号

               不支持undefined

      

   

     3.数组形式

      

 

 

        JSON的数组形式就对应着JS中的数组

     注意

        数组中的字符串必须用双引号

        JSON只要涉及到字符串,就必须使用双引号

        不支持undefined 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JSON 的 3 种形式</title>
    </head>
    <body>
        <script>
        /*     const xhr =new XMLHttpRequest();
            
            xhr.onreadystatechange = () =>{
                if(xhr.readyState!== 4) return;
                
                if(xhr.status>= 200&&xhr.status < 300 ||xhr.status===304){
                    console.log(xhr.responseText);
                    console.log(typeof xhr.responseText)
                }
            };
            xhr.open('GET','./json/plain.json',true);
            xhr.send(null); */
            
        /*     const xhr =new XMLHttpRequest();
                
                xhr.onreadystatechange = () =>{
                    if(xhr.readyState!== 4) return;
                    
                    if(xhr.status>= 200&&xhr.status < 300 ||xhr.status===304){
                        console.log(xhr.responseText);
                        console.log(typeof xhr.responseText)
                    }
                };
                //xhr.open('GET','./json/plain.json',true);
                xhr.open('GET','./json/ojb.json',true);
                xhr.send(null); */
                
                const xhr =new XMLHttpRequest();
                    
                    xhr.onreadystatechange = () =>{
                        if(xhr.readyState!== 4) return;
                        
                        if(xhr.status>= 200&&xhr.status < 300 ||xhr.status===304){
                            console.log(xhr.responseText);
                            console.log(typeof xhr.responseText)
                        }
                    };
                    //xhr.open('GET','./json/plain.json',true);
                    //xhr.open('GET','./json/ojb.json',true);
                    xhr.open('GET','./json/arr.json',true);
                    xhr.send(null);
                    //1,"Loveweiwei",null
        </script>
    </body>
</html>