其他分享
首页 > 其他分享> > JSON概念和语法定义

JSON概念和语法定义

作者:互联网

JSON概念

概念:JavaScript Object Notation JavaScript对象表示法

  1. json现在多用于存储和交换文本信息的语法
  2. 进行数据的传递
  3. JSON比XML更小、更快,更易解析

JSON语法定义

基本规则

 HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Json数据语法</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script>
        //定义基本格式
        var person = {"name": "张三", age: 23, 'gender': true};
        //alert(person)
        //2.嵌套格式
        var persons = {
            "persons":
                [{"name": "张三", "age": 23, 'gender': true},
                {"name": "张三", "age": 23, 'gender': true},
                {"name": "张三", "age": 23, 'gender': true}]
        };
        alert(persons);

        //嵌套格式[]->{}
        var ps=[
            [{"name": "张三", "age": 23, 'gender': true},
            {"name": "张三", "age": 23, 'gender': true},
            {"name": "张三", "age": 23, 'gender': true}]
        ];
        alert(ps)
    </script>
</head>
<body>

</body>
</html>

运行结果

 

 获取数据

单独获取数据

  1. json对象.键名
  2. json对象["键名"]
  3. 数组对象[索引]

HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Json数据语法</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script>
        //定义基本格式
        var person = {"name": "张三", age: 23, 'gender': true};
        //获取name的值
        var name=person["name"];
        alert(name)
        //2.嵌套格式
        var persons = {
            "persons":
                [{"name": "张三", "age": 23, 'gender': true},
                {"name": "李四", "age": 23, 'gender': true},
                {"name": "王五", "age": 23, 'gender': true}]
        };
        //alert(persons);
        //获取王五的值
        var name1 = persons.persons[2].name;
        alert(name1);

        //嵌套格式[]->{}
        var ps=[
            {"name": "张三", "age": 23, 'gender': true},
            {"name": "李四", "age": 23, 'gender': true},
            {"name": "王五", "age": 23, 'gender': true}
        ];
        //alert(ps)
        //获取李四的值
        alert(ps[1].name);

    </script>
</head>
<body>

</body>
</html>

运行结果

遍历获取数据

HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Json数据语法</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script>
       //1.定义基本格式
        var person={"name":"张三",age:23,'gender':true};

        var ps=[
            {"name":"张三","age":23,"gender":true},
            {"name":"李四","age":24,"gender":true},
            {"name":"王五","age":25,"gender":false},
            ];

       //获取person对象中所有的键和值
       //for in 循环
        for(var key in person){
            //这样的方式获取不行。因为相当于  person."name"
            //alert(key + ":" + person.key);
            alert(key+":"+person[key]);
        }

       //获取ps中的所有值
       for (var i = 0; i < ps.length; i++) {
           var p = ps[i];
           for(var key in p){
               alert(key+":"+p[key]);
           }
       }

    </script>
</head>
<body>

</body>
</html>

标签:定义,23,gender,age,语法,JSON,var,true,name
来源: https://www.cnblogs.com/aimz01/p/16609649.html