首页 > 其他分享 >初识JSON&JSON的三种形式&JSON的常用方法

初识JSON&JSON的三种形式&JSON的常用方法

时间:2023-03-29 11:14:21浏览次数:33  
标签:console log json xhr JSON 初识 三种 responseText

初识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的三种形式

 

   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> 

 

 

JSON的常用方法

 

  JSON.parse()

      

复制代码
console.log(JSON.parse(xhr.responseText));  
复制代码 复制代码
console.log(JSON.parse(xhr.responseText).data);
复制代码

 

 

复制代码
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JSON 的常用方法</title>
    </head>
    <body>
        <script>
                //1.JSON.parse()
                //JSON.parse()可以将JSON格式的字符串解析成js中的对应值
                //一定要是合法的JSON字符串,否则会报错
            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)
                    
                        console.log(JSON.parse(xhr.responseText));
                            
                        /* console.log(JSON.parse(xhr.responseText).data); */
                    }
                };
            
                //xhr.open('GET','./json/plain.json',true);
                xhr.open('GET','./json/arr.json',true);
                /* xhr.open('GET','https://www.imooc.com/api/http/search/suggest?words=js',true); */
                xhr.send(null);
        </script>    
    </body>
</html>
复制代码

    JSON.stringify()

        

 

 

 

    使用JSON.parse()和JSON.stringify()封装localStorage

   

复制代码
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JSON 的常用方法</title>
    </head>
    <body>
        <script type="module">
                //1.JSON.parse()
                //JSON.parse()可以将JSON格式的字符串解析成js中的对应值
                //一定要是合法的JSON字符串,否则会报错
            // 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)
                    
            //             console.log(JSON.parse(xhr.responseText));
                            
            //             /* console.log(JSON.parse(xhr.responseText).data); */
            //         }
            //     };
            
            //     //xhr.open('GET','./json/plain.json',true);
            //     xhr.open('GET','./json/arr.json',true);
            //     /* xhr.open('GET','https://www.imooc.com/api/http/search/suggest?words=js',true); */
            //     xhr.send(null);
            
            
            //2.JSON.stringify()
            //JSON.JSON.stringify()可以JS的基本数据类型、对象或者数组
            //转换成JSON格式的字符串
                /* console.log(JSON.stringify({
                    uusername:'alex',
                    age:18,
                })
                ); */
            
            /*     const xhr = new XMLHttpRequest();
                
                
                xhr.open('post','https://www.imooc.com/api/http/search/suggest?words=js',true);
                
                xhr.send(
                    JSON.stringify({
                            username:'alex',
                            age:18,
                    })
                ); */
                
            //3.使用JSON.parse()和JSON.stringify() 封装
                localStorage    
                import{get,set,remove,clear} from './js/storage.js';
                
                set('username','nmb');
                console.log(get('username'));
                
                
                set('nmb2.0',{
                    name:'那么棒',
                    age:18
                });
                console.log(get('nmb2.0'));
                
                remove('username');
                
                clear();
                
        </script>    
    </body>
</html> 
复制代码

 

 

    

 

 

 

标签:console,log,json,xhr,JSON,初识,三种,responseText
From: https://www.cnblogs.com/yu3304/p/17268159.html

相关文章

  • package.json 配置详解
    package.json配置说明详解基础配置{"name":"my-package",//包名必填字段"version":"1.0.0",//版本号必填字段"description":"Thisismyfirstnpmpa......
  • Mybatis-Plus自定义TypeHandler映射JSON类型为List
    1.实体类注意点:别忘了autoResultMap=true@Data@TableName(value="report",autoResultMap=true)publicclassReportimplementsSerializable{privates......
  • JSON 数组对象获取 其中某个属性的值
    importcn.hutool.json.JSONArray;importcn.hutool.json.JSONObject;importcn.hutool.json.JSONUtil;publicstaticvoidmain(String[]args){Strings......
  • 第六篇 TypeScript 【 typeScript 编译上下文 tsconfig.json 】
    typeScript编译上下文tsconfig.json的作用1、用于标识TypeScript项目的根路径2、用于配置TypeScript编译器3、用于指定编译的文件tsconfig.json重要字段1......
  • javascript 使用json 将js 数据转换成json
     如果是字符串格式的话接的先解析成jsonvarjsonList=pm.response.text();varjson=JSON.parse(jsonList);console.log(json);console.log(json.has_more);varres={"......
  • Java JSON库Jackson 2.x新变化一览
    《JavaJSON库Jackson2.x新变化一览》作者:chszsJackson库是JSONJava库,用于在Java程序中解析JSON数据。Jackson库于2012.10.8号发布了最新的2.1版。由于有不少变化,这里做一......
  • 3-1.3-3初识localStorage|3-5的localStorage的注意事项|课程总结
    localStorage是什么localStorage也是一种浏览器储存数据的方式(本地储存),他只是存储在本地,不会发送到客户端单个域名下的localStorage总大小有限制lo......
  • Qt Json的使用教程
    前言从Qt5.0就开始提供处理Json数据的操作支持,JSON是一种对源自Javascript的对象数据进行编码的格式,但现在广泛用作互联网上的数据交换格式,Qt中的JSON支持提供了......
  • JSON的基本使用(详解)
    一、什么是JSONJSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于Ja......
  • 一、初识Java
    学习目标了解Java语言的特点掌握Java环境变量的配置熟悉Java的运行机制掌握Eclipes/Idea开发工具的使用是计算机、移动设备、家用电器等领域最受欢迎的开发语言之一......