节选自:http://www.json.cn/wiki.html
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。 JSON采用独立于编程语言的文本格式,但是也使用了类C语言的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。
json简单说就是javascript中的对象和数组,因此JSON也基于两种结构表示各种复杂结构:
对象:
对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,...}的键值对的结构,取值方法为 对象.key 获取属性值,这个属性值的类型可以是 数字、字符串、数组、对象几种。无序。
数组:
数组在js中是中括号“[]”括起来的内容,数据结构为 ["java","javascript","vb",...],使用索引获取,字段值的类型可以是 数字、字符串、数组、对象几种。有序。
注意:
1-JSON中规定key必须使用双引号
2-访问数据可使用点号法
举例:
{
"animals": {
"dog": [
{
"name": "Rufus",
"age":15
},
{
"name": "Marty",
"age": null
}
]
}
}
原文链接:https://blog.csdn.net/u010569893/article/details/97538914
https://blog.csdn.net/qq_36117775/article/details/104226682
字典和JSON的格式很像,都是key:value形式,并且JSON和dict也可以使用dumps与loads相互转换,但它们还是有区别的。
dict是一种数据结构,而JSON那是一种数据格式,作为格式JSON会有很多形式限制,比如必须使用双引号作为边界符号,但字典可以使用单引号也可以使用双引号。
dict作为完整的数据结构,是对Hash Table这一数据结构的一种实现,是一套从存储到提取都封装好了的方案。
[]
,也可以是.
,遍历方式分in、of;dict的value仅可以下标访问。JSON格式与dict转换json.loads(json_str)
:json字符串转换成字典json.dumps(dict)
:字典转换成json字符串
保证数据的安全
客户端是不可信的,如果服务器程序直接将有缺陷的JSON字符串覆盖回用户对应的JSON文件里,那么这个用户可能每次打开以这个账户登录APP都会闪退,甚至只能重新申请一个新的账号。
维护服务器的安全
如果一个恶意攻击者将一段包含Shell命令的字符串——而不是正确的JSON字符串——传到了服务器。那么可能他只需用这个账户登录APP就可以获得服务器的控制权。
因此,无论出于哪方面考虑,我们都必须对从客户端传到服务器的JSON字符串进行校验。
节选自:https://www.cnblogs.com/superhin/p/11230376.html
对于JSON格式的请求数据或者响应数据,在不同的数据和场景下往往会有一部分动态的值及字段。此时我们可以使用JSON Scheme Validator(JSON结构验证)来验证JSON的结构,各参数及嵌套参数的类型,以及必要字段。
举例:GET http://httpbin.org/get?a=a
的响应数据:
{
"args": {
"a": "a"
},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-5fc5ddda-0a9874912db7168035ceee76"
},
"origin": "60.216.115.94",
"url": "http://httpbin.org/get?a=a"
}
我们可以验证其结构为:
因此使用JSON Schema语法可写为:
{
"type": "object",
"properties": {
"args": {"type": "object","properties": {"a": {"type": "string"}} },
"hearders": {
"type": "object",
"properties": {
"Accept": {"type": "string"},
"Accept-Encoding": {"type": "string"},
"Accept-Language": {"type": "string"},
"Host": {"type": "string"},
"Upgrade-Insecure-Requests": {"type": "string"},
"User-Agent": {"type": "string"},
"X-Amzn-Trace-Id": {"type": "string"},
},
},
"origin": {"type": "string"},
"url": {"type": "string"},
},
"required": ["args", "headers", "origin", "url"]
}
【object类型】
格式:
{
"type": "object",
"properties": {...}
"required": [....]
}
其中type类型指定为object, properties下写各个子属性,required中填写必须要出现的元素,required中未注明的元素可以不出现,但出现则必须是限定的类型
【Array类型】
格式:
{
"type": "array",
"items": {...}
"required": [....]
}
其中type类型为array, items下写各个子项, required中填写必须要出现的元素。
【string类型】
{"type": "string"}
【integer类型】
{"type": "integer"}
【boolean类型】
{"type":"boolean"}
详细可以参考:http://json-schema.org/
选自:https://www.cnblogs.com/superhin/p/11230376.html
https://www.softwaretestingmaterial.com/json-schema-validation-in-postman/
tv4即 Tiny Validator for JSON data的缩写,微型JSON结构验证器。
在Postman中的使用方法也很简单,首先在Tests脚本中根据响应编写JSON Schema结构模板,然后使用tv4.validate(jsonData, schema)进行验证即可.
同样以GET http://httpbin.org/get?a=a
为例,Postman中Tests脚本代码如下:
var schema = {
"type":"object",
"properties":{
"args":{"type":"object","properties":{"a":{"type":"string"}}},
"headers":{
"type":"object",
"properties":{
"Accept":{"type":"string"},
"Accept-Encoding":{"type":"string"},
"Accept-Language":{"type":"string"},
"Host":{"type":"string"},
"Upgrade-Insecure-Requests":{"type":"string"},
"User-Agent":{"type":"string"},
"X-Amzn-Trace-Id":{"type":"string"},
}},
"origin":{"type":"string"},
"url":{"type":"string"},
},
"required":["args","headers","origin","url"]
}
pm.test("Schema is valid", function () {
var jsonData = pm.response.json();
pm.expect(tv4.validate(jsonData,schema)).to.be.true;
});
send后查看断言,显示通过:
JSONSchema验证.png
现在也可使用工具,自动为JSON数据编写JSON Schema语法:
打开https://jsonschema.net/home
粘贴JSON数据
点击Submit
它会自动生成对应的schema
复制schema并粘贴到Postaman的Tests模块中来实现schema验证
同时添加下面的代码
pm.test("Schema is valid", function () {
pm.response.to.have.jsonSchema(schema)
});
这样就为返回数据创造了一个JSON schema,Postman会使用它验证返回的JSON数据,如果返回数据与schema设置的值不相符,test就会失败
如果不想在返回的JSON数据中看到没有定义过的属性,可以在schema中添加一个额外设置:additionalProperties
,如果将它设置为False,则不支持没定义过的附加属性。
举例:
const schema = {
"type":"object",
"properties":{"code":{"type":"string"}},
"required":["args","headers","origin","url"],
"additionalProperties":false
}