zoukankan      html  css  js  c++  java
  • JSON代码书写规范

      在AJAX实现前后台数据交互的时候,通常使用JSON的数据格式,对于JSON来说,有严格的代码规范,一旦格式出问题,就无法显示出相应效果,同时还不在控制台报错。那么JSON的书写有哪些规范。

    JSON是什么?

      在前后台的交互中,通常要互相传递消息,那就需要一种两方面都能“听懂的语言”,数据格式这里就代表语言。JSON就是前后台中都能理解的一种“语言”。

    JSON的类型

      JSON也有不同的组织形式,一种是JSON对象,一种为JSON数组。因此,在书写的代码当中,需要遵循基本的对象、数组的书写方式。

    1.数组方式

     1         [{
     2             "city" : "BeiJing",
     3             "num" : 5
     4         }, {
     5             "city" : "ShenZhen",
     6             "num" : 5
     7         }, {
     8             "city" : "XiaMen",
     9             "num" : 5
    10         }]

    2.对象方式

     1         {
     2             "user" : "ZhangSan",
     3 
     4             "type" : "work",
     5 
     6             "team" : [{
     7                 "city" : "BeiJing",
     8                 "num" : 3
     9             }, {
    10                 "city" : "GuangZhou",
    11                 "num" : 3
    12             }, {
    13                 "city" : "ShangHai",
    14                 "num" : 3
    15             }]
    16         }

    书写JSON的注意事项

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

    {'user' : 'zhangsan'}//不合法
    {"user": 'zhangsan'}//不合法

    2. 对象的成员名称必须使用双引号

    {"user" : "zhangsan"}//合法

    3. 数组或对象最后一个成员的后面,不能加逗号

    1         [{
    2             "city" : "BeiJing",
    3             "num" : 5,//不合法
    4         }, {
    5             "city" : "ShenZhen",
    6             "num" : 5,//不合法
    7         }]

    4. 数组或对象的每个成员的值,可以是简单值,也可以是复合值。简单值分为四种:字符串、数值(必须以十进制表示)、布尔值和null(NaN, Infinity, -Infinity和undefined都会被转为null)。复合值分为两种:符合JSON格式的对象和符合JSON格式的数组。

    {"age" : ox16}//不合法,数值必须是十进制的
    {"city" : undefined}//使用undefined,不合法
    1 {"city" : null,
    2 "getcity": function() {
    3   console.log("错误用法");
    4 }}//JSON中不能使用自定义函数或系统内置函数(如Date())
  • 相关阅读:
    LeetCode 842. Split Array into Fibonacci Sequence
    LeetCode 1087. Brace Expansion
    LeetCode 1219. Path with Maximum Gold
    LeetCode 1079. Letter Tile Possibilities
    LeetCode 1049. Last Stone Weight II
    LeetCode 1046. Last Stone Weight
    LeetCode 1139. Largest 1-Bordered Square
    LeetCode 764. Largest Plus Sign
    LeetCode 1105. Filling Bookcase Shelves
    LeetCode 1027. Longest Arithmetic Sequence
  • 原文地址:https://www.cnblogs.com/hhsy/p/5643245.html
Copyright © 2011-2022 走看看