JSON必知必会阅读 | 您所在的位置:网站首页 › json必知必会 › JSON必知必会阅读 |
阅读了一下JSON必知必会,由于日常使用JSON比较多,所以文中内容大部分都已经接触过了,大概通读全书耗时1h左右 PDF地址:链接:https://pan.baidu.com/s/1iZQLR1zGJbH9Eoq3t_GYFQ 密码:x4lv 基础JSON基于JavaScript对象字面量 JSON的全称是 JavascriptObjectNotation「Javascript 对象表示法」 不过它是一种数据交换格式,是独立于语言的。 语法JSON中使用冒号 :来分隔名称和值始终需要被 双引号包裹全部内容需要被花括号包裹 {}{:开始读取对象 }:结束读取对象 [:开始读取数组 ]:结束读取数组 ::在名称-键值中分隔名称和值 ,:分隔「对象中的名称-值对」或「数组中的值」 语法验证集成开发环境「IDE」自带JSON Formatter & Validator: https://jsonformatter.curiousconcept.comJSON Editor Online:http://www.jsoneditoronline.orgJSONLint:https://jsonlint.comJSON的媒体类型传递数据的时候,需要提前告知接收方数据是什么类型的。媒体类型也有一些其他的称呼:「互联网媒体类型」,「内容类型」,「MIME类型」 它使用「类型/子类型」 JSON的MIME类型是 application/json全部媒体类型列表可见:互联网数字分配机构「IANA」维护的列表 http://www.iana.org/assignments/media-types/media-types.xhtml 专业术语字面量:字面意思和与其想要表达的意思是完全一致的值 最大可移植性:通过保证数据本身对平台和系统的兼容性来提供超越数据格式本身的可移植性 一致性验证:关注独特数据结构的验证 JSON的数据类型原始数据类型数字整型 浮点数 定点数 字符和字符串布尔类型复合数据类型 由原始数据类型融合而成。 JSON中的数据类型 对象字符串数字布尔值null数组转义 双引号 错误 { “promo”:"Say "Bob`s the best!” at checkout for free 8oz bag of kibble.” }当一个值以「“」开始时,它希望接下来的字符串文本以另一个双引号结尾 正确{ “promo”:"Say \"Bob`s the best!\” at checkout for free 8oz bag of kibble.” }反斜线\\ 正斜线\/ 退格符\b 换页符\f 制表符\t \t-> \\t 换行符\n 回车符\r 十六进制字符\u后面跟十六进制字符 布尔值JSON中布尔值仅使用小写形式:true, false null在编程中,null就用来表示 ~0~ , ~一无所有~ , ~不存在~ 等意思 在JSON中 null必须小写 不要混淆 null和 undefined undefined不是JSON中的数据类型 数组数组始终应该被方括号 []包裹。在数组中,可以看到一个列表,列表项之间用「逗号」隔开。每个列表项都是合法的JSON数据类型 JSON SchemaSchema「模式」 JSON验证器负责 ~验证语法错误~ ,JSON Schema负责提供 ~一致性校验~ 。JSON Schema是数据接收方的第一道防线,也是数据发送方节约时间,保证数据正确的好工具。JSON Schema可以解决下列有关一致性验证的问题:值的数据类型是否正确具体规定一个值是数字/字符串等类型 是否包含所需要的数据 具体规定哪些数据是需要的,哪些是不需要的值的形式是不是我需要的 指定范围,最大值和最小值语法 JSON Schema | The home of JSON Schema:https://json-schema.org/ 例子JSON Schema本身就是一个JSON对象 { “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是否符合要求 { “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 documentation:https://json-schema.org/understanding-json-schema/index.html 网上中文学习材料JSON Schema 详解_Bossen的学习历程-CSDN博客:https://blog.csdn.net/swinfans/article/details/89231247‘ 通过Python进行JSON模式校验安装JSON校验模块 pip install jsonschema使用官网例子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” } }校验validate(instance=test1, schema=schema) validate(instance=test2, schema=schema)校验 test2的时候没有报错 校验 test1的时候报错 Traceback (most recent call last): File "/Users/zhongxin/Desktop/ApiTest/test/20200203.py", line 55, in 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」是一种利用站点对用户浏览器信任而发起攻击的方式。浏览器虽然对不同域名之间的站点资源分享有一定的限制规则,但是可以通过 标签来绕开。在你完成某网站的登陆之后,浏览器会保存你与该网站的凭证。在此时如果访问包含危险 标签的网站,那么你的凭证信息就可能会被窃取利用。 防护不在JSON中使用顶级数组不要贪图GET代替POST的便利注入攻击跨站脚本攻击「XSS」是注入攻击的一种 如果在JavaScript编程中使用 eval()来对对象进行转换,那么就很有可能进行恶意代码的执行 防护一般使用 JSON.parse()函数代替 eval()函数 |
CopyRight 2018-2019 实验室设备网 版权所有 |