JSON必知必会

JSON必知必会

阅读了一下JSON必知必会,由于日常使用JSON比较多,所以文中内容大部分都已经接触过了,大概通读耗时1h左右
PDF地址:
链接:https://pan.baidu.com/s/1iZQLR1zGJbH9Eoq3t_GYFQ 密码:x4lv

基础

JSON基于JavaScript对象字面量

JSON的全称是Javascript Object Notation「Javascript 对象表示法」
不过它是一种数据交换格式,是独立于语言的。

语法

  • JSON中使用冒号:来分隔名称和值
  • 始终需要被双引号包裹
  • 全部内容需要被花括号包裹{}

    {:开始读取对象
    }:结束读取对象
    [:开始读取数组
    ]:结束读取数组
    ::在名称-键值中分隔名称和值
    ,:分隔「对象中的名称-值对」或「数组中的值」

语法验证

  1. 集成开发环境「IDE」自带
  2. JSON Formatter & Validator: https://jsonformatter.curiousconcept.com
  3. JSON Editor Online:http://www.jsoneditoronline.org
  4. JSONLint:https://jsonlint.com

    JSON的媒体类型

    传递数据的时候,需要提前告知接收方数据是什么类型的。
    媒体类型也有一些其他的称呼:「互联网媒体类型」,「内容类型」,「MIME类型」
    它使用「类型/子类型」
    JSON的MIME类型是application/json
    全部媒体类型列表可见:互联网数字分配机构「IANA」维护的列表
    http://www.iana.org/assignments/media-types/media-types.xhtml

    专业术语

    字面量:字面意思和与其想要表达的意思是完全一致的值
    最大可移植性:通过保证数据本身对平台和系统的兼容性来提供超越数据格式本身的可移植性
    一致性验证:关注独特数据结构的验证

JSON的数据类型

原始数据类型

  • 数字

    整型
    浮点数
    定点数

  • 字符和字符串
  • 布尔类型

    复合数据类型

    由原始数据类型融合而成。

    JSON中的数据类型

  • 对象
  • 字符串
  • 数字
  • 布尔值
  • null
  • 数组

    转义

    双引号

    错误
    1
    2
    3
    {
    “promo”:"Say "Bob`s the best!” at checkout for free 8oz bag of kibble.”
    }
    当一个值以「“」开始时,它希望接下来的字符串文本以另一个双引号结尾
    正确
    1
    2
    3
    {
    “promo”:"Say \"Bob`s the best!\” at checkout for free 8oz bag of kibble.”
    }

    反斜线

    \\

    正斜线

    \/

    退格符

    \b

    换页符

    \f

    制表符

    \t
    \t->\\t

    换行符

    \n

    回车符

    \r

    十六进制字符

    \u后面跟十六进制字符

    布尔值

    JSON中布尔值仅使用小写形式:truefalse

    null

    在编程中,null就用来表示 0一无所有不存在 等意思
    在JSON中null必须小写
    不要混淆nullundefined
    undefined不是JSON中的数据类型

    数组

    数组始终应该被方括号[]包裹。在数组中,可以看到一个列表,列表项之间用「逗号」隔开。每个列表项都是合法的JSON数据类型

    JSON Schema

    Schema「模式」
  1. JSON验证器负责 验证语法错误 ,JSON Schema负责提供 一致性校验
  2. JSON Schema是数据接收方的第一道防线,也是数据发送方节约时间,保证数据正确的好工具。
  3. JSON Schema可以解决下列有关一致性验证的问题:
  • 值的数据类型是否正确

    具体规定一个值是数字/字符串等类型

  • 是否包含所需要的数据

    具体规定哪些数据是需要的,哪些是不需要的

  • 值的形式是不是我需要的

    指定范围,最大值和最小值

    语法

    JSON Schema | The home of JSON Schemahttps://json-schema.org/

例子

JSON Schema本身就是一个JSON对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
“type”: "object”,
“properties”: {
“first_name”: { "type": "string" },
"last_name”: { “type”: “string” },
“birthday”: { “type”: "string”, “format": "date” },
“address": {
“type”: "object”,
“properties”: {
“street_address”: { “type”: “string” },
“city”: { “type”: “string” },
“state”: { “type”: “string” },
“country”: { “type” : “string” }
}
}
}
}

上面的JSON Schema可以校验如下JSON是否符合要求

1
2
3
4
5
6
7
8
9
10
11
{
“first_name”: “George",
"last_name": "Washington”,
“birthday”: “1732-02-22”,
“address”: {
“street_address”: “3200 Mount Vernon Memorial Highway”,
“city”: "Mount Vernon",
“state": “Virginia",
“country”: “United States”
}
}

由于概念比较简单但是比较多,直接可以查看官方文档,或者他人的翻译
官方英文文档:
Understanding JSON Schema — Understanding JSON Schema 7.0 documentationhttps://json-schema.org/understanding-json-schema/index.html
网上中文学习材料
JSON Schema 详解_Bossen的学习历程-CSDN博客https://blog.csdn.net/swinfans/article/details/89231247‘

通过Python进行JSON模式校验

  1. 安装JSON校验模块
    pip install jsonschema
  2. 使用官网例子
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    from jsonschema import validate

    schema = {
    type”: “object",
    "properties": {
    “first_name”: {“type”: “string”},
    “last_name": {“type”: “string"},
    “birthday": {“type”: “string", “format”: "date”},
    “address": {
    “type": “object”,
    “properties”: {
    “street_address”: {“type”: “string”},
    "city”: {“type”: “string”},
    "state": {"type": "string"},
    “country”: {“type”: “string”}
    }
    }
    }
    }
    test1 = {
    “name": “George Washington”,
    “birthday”: “February 22, 1732",
    “address”: “Mount Vernon, Virginia, United States”
    }
    test2 = {
    “first_name”: “George”,
    “last_name”: “Washington”,
    “birthday”: “1732-02-22”,
    “address”: {
    “street_address”: “3200 Mount Vernon Memorial Highway”,
    “city”: “Mount Vernon”,
    “state”: “Virginia”,
    “country”: “United States”
    }
    }
  3. 校验
    1
    2
    validate(instance=test1, schema=schema)
    validate(instance=test2, schema=schema)
    校验test2的时候没有报错
    校验test1的时候报错
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    Traceback (most recent call last):
    File "/Users/zhongxin/Desktop/ApiTest/test/20200203.py", line 55, in <module>
    test_1()
    File "/Users/zhongxin/Desktop/ApiTest/test/20200203.py", line 47, in test_1
    validate(instance=test1, schema=schema)
    File "/Users/zhongxin/.local/share/virtualenvs/LearnDjango/lib/python3.7/site-packages/jsonschema/validators.py", line 934, in validate
    raise error
    jsonschema.exceptions.ValidationError: 'Mount Vernon, Virginia, United States' is not of type 'object'

    Failed validating 'type' in schema['properties']['address']:
    {'properties': {'city': {'type': 'string'},
    'country': {'type': 'string'},
    'state': {'type': 'string'},
    'street_address': {'type': 'string'}},
    'type': 'object'}

    On instance['address']:
    'Mount Vernon, Virginia, United States'

    JSON中的安全问题

    JSON本身不会构成什么威胁,它只是文本。不过由于它广泛应用于web相关,所以会有常见的两个安全问题:「跨站请求伪造」「跨站脚本攻击」

    跨站请求伪造

    「CSRF」是一种利用站点对用户浏览器信任而发起攻击的方式。
    浏览器虽然对不同域名之间的站点资源分享有一定的限制规则,但是可以通过<script>标签来绕开。
    在你完成某网站的登陆之后,浏览器会保存你与该网站的凭证。在此时如果访问包含危险<script>标签的网站,那么你的凭证信息就可能会被窃取利用。

    防护

  • 不在JSON中使用顶级数组
  • 不要贪图GET代替POST的便利

    注入攻击

    跨站脚本攻击

    「XSS」是注入攻击的一种
    如果在JavaScript编程中使用eval()来对对象进行转换,那么就很有可能进行恶意代码的执行

    防护

    一般使用JSON.parse()函数代替eval()函数
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!