您当前的位置: 首页 >  json

dawn

暂无认证

  • 5浏览

    0关注

    204博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Json(1):基础知识

dawn 发布时间:2021-10-17 22:14:48 ,浏览量:5

  Json(JavaScript·Object·Notation)是一种轻量级、纯文本的用于数据存储、传递和交换的表示方法信息。   ①JSON的语法简单灵活,衍生于 JavaScript 对象标记法语法:   ●数据在名称/值对中   ●数据由逗号分隔   ●花括号容纳对象   ●方括号容纳数组   ②在 JSON 中,值必须是以下数据类型之一:   字符串、数字、对象(JSON 对象)、数组、布尔、null   JSON 文件的文件类型是 ".json",JSON 文本的 MIME 类型是 "application/json"   ③它可以传输任何数据类型的数据。   当数据在浏览器与服务器之间进行交换时,这些数据只能是文本。   我们能够把任何 JavaScript 对象转换为 JSON,然后将 JSON 发送到服务器。我们也能把从服务器接收到的任何 JSON 转换为JavaScript 对象。以这样的方式,我们能够把数据作为 JavaScript 对象来处理,无需复杂的解析和转译。

  以实际的代码操作来学习。

  (一)单个对象

  先使用JavaScript来声明、赋值,然后输出。




    
    
    
    Json学习
    
        // 声明和赋值
        var student={
            id:101,
            name:'小明',
            ChineseScore:81.5,
            MathScore:87,
            totalScore:function(){
                return this.ChineseScore+this.MathScore;
            },
        };
        //信息显示
        function showInfo(){
            document.write("  学号:"+student.id);
            document.write("  姓名:"+student.name);
            document.write("  语文:"+student.ChineseScore);
            document.write("  数学:"+student.MathScore);
            document.write("  总分:"+student.totalScore());
        };
    


    

  输出结果:

   如果上面的对象转换成Json格式定义和输出:




    
    
    
    Json学习
    
        // 声明和赋值
        var student={
            id:101,
            name:'小明',
            ChineseScore:81.5,
            MathScore:87,
            totalScore:function(){
                return this.ChineseScore+this.MathScore;
            },
        };
        //信息显示
        function showInfo(){
            var jsonStudent=JSON.stringify(student);
            document.write(jsonStudent);
        };
    


    

  输出为:

  方法不见了,这意味着如果再转换为对象,totalScore没有了。

   如果我们按照Json的格式来形成字符串,可以把方法放进去。




    
    
    
    Json学习
    
        function showInfo(){
            //对象字符串
            var strJson='{"student":{"id":101,"name":"小明","ChineseScore":81.5,"MathScore":87,"totalScore":function(){return this.ChineseScore+this.MathScore;}} }';
            //转换为JavaScript对象
            var jsonObj=eval("("+strJson+")");
            document.write("  学号:"+jsonObj.student.id);
            document.write("  姓名:"+jsonObj.student.name);
            document.write("  语文:"+jsonObj.student.ChineseScore);
            document.write("  数学:"+jsonObj.student.MathScore);
            document.write("  总分:"+jsonObj.student.totalScore());
      };
    


    

  输出结果:

  为什么eval要加大括号?

  因为eval可以处理表达式而不能处理语句块,加大括号强制将括号内的表达式转换为对象。

            var strObj="{}";

            console.log(typeof eval(strObj));//输出为underfined

            console.log(typeof eval('('+strObj+')'));输出为Object

            console.log(typeof JSON.parse(strObj));//输出为Object

  说明Json格式的严格要求,单个对象必须是

  '{"对象名":{"属性名":属性值,"属性名":属性值,"属性名":属性值,"函数名":函数内容}

  这样才能被eval正确解析。

  eval显然比JSON.parse强大得多。

  上面的转换如果用JSON.parse来进行则最后的totalScore也报错。

  (二)对象数组

  对象数组用的多,比如表格数据。




    
    
    
    Json学习
    
        function showInfo(){
            //创建studentList对象
            var studentList=[
                            {id:101,name:'小明',chineseScore:81.5,mathScore:87,totalScore:function(){ return this.chineseScore+this.mathScore;}},
                            {id:102,name:'小黄',chineseScore:61,mathScore:47.5,totalScore:function(){ return this.chineseScore+this.mathScore;}},
                            {id:103,name:'小丽',chineseScore:89.5,mathScore:83,totalScore:function(){ return this.chineseScore+this.mathScore;}},
                            {id:104,name:'小宋',chineseScore:56,mathScore:97,totalScore:function(){ return this.chineseScore+this.mathScore;}},
                        ];
            //输出
            for(var i=0;i            
关注
打赏
1664252102
查看更多评论
0.0485s