JSON code writing specification

    When AJAX realizes front-end and back-end data interaction, JSON data format is usually used. For JSON, there are strict code specifications. Once there is a problem with the format, the corresponding effect cannot be displayed, and an error is not reported in the console. So what are the specifications for JSON writing?

What is JSON?

  In front-end and back-end interactions, messages are usually communicated to each other, which requires a language that can be "understood" in both aspects. The data format here represents the language. JSON is a "language" that can be understood by both front and back.

Types of JSON

  JSON also has different forms of organization, one is JSON object, the other is JSON array. Therefore, in the written code, it is necessary to follow the basic way of writing objects and arrays.

1. Array method

[{

            "city" : "BeiJing",

            "num" : 5

        }, {

            "city" : "ShenZhen",

            "num" : 5

        }, {

            "city" : "XiaMen",

            "num" : 5

        }]

2. Object method

{

            "user" : "ZhangSan",

 

            "type" : "work",

 

            "team" : [{

                "city" : "BeiJing",

                "num" : 3

            }, {

                "city" : "GuangZhou",

                "num" : 3

            }, {

                "city" : "ShangHai",

                "num" : 3

            }]

 

        }

书写JSON的注意事项

1. 数组或对象之中的字符串必须使用双引号,不能使用单引号

 

{'user' : 'zhangsan'}//不合法
{"user": 'zhangsan'}//不合法
2. 对象的成员名称必须使用双引号
{"user" : "zhangsan"}//合法
3. 数组或对象最后一个成员的后面,不能加逗号
[{
            "city" : "BeiJing",
            "num" : 5,//不合法
        }, {
            "city" : "ShenZhen",
            "num" : 5,//不合法
        }]
4. 数组或对象的每个成员的值,可以是简单值,也可以是复合值。简单值分为四种:字符串、数值(必须以十进制表示)、布尔值和null(NaN, Infinity, -Infinity和undefined都会被转为null)。复合值分为两种:符合JSON格式的对象和符合JSON格式的数组。
{"age" : ox16}//不合法,数值必须是十进制的
{"city" : undefined}//使用undefined,不合法
{"city" : null,
"getcity": function() {
 console.log("错误用法");
}}//JSON中不能使用自定义函数或系统内置函数(如Date())

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326654069&siteId=291194637