Json.Net使用JSON Schema验证JSON格式【实例】
给出一个Json,验证其格式是否符合规则。
{
"coord": { //对象
"lon": 145.77,
"lat": -16.92
},
"sys": { //对象
"type": ,
"id": ,
"message": 0.0402,
"country": "AU",
"sunrise": ,
"sunset":
},
"weather": [ //数组(子项是对象)
{
"id": ,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
},
{
"id": ,
"main": "Clouds",
"description": "few clouds",
"icon": "02d"
}
],
"base": "cmc stations", //字符串
"main": { //对象
"temp": 304.15, //浮点型
"pressure": , //整形
"humidity": , //整形
"temp_min": 304.15,
"temp_max": 304.15
},
"wind": { //对象
"speed": 5.7,
"deg":
},
"clouds": {
"all":
},
"dt": , //整形
"id": , //整形
"name": "Cairns", //字符串
"cod": 200 //整形
}
在前一篇文中我们知道JSON Schema可以通过加载字符串或者文件得到,可是新手一下子写出验证的字符串实在有点难度。
还好,Json.Net里面可以在代码里创建JSON Schema,简直是手把手教学,显浅易懂。
参见上一篇文中的代码里创建JSON Schema,我们将这个Json分拆为coord、sys、weather、base、main、wind、clouds、dt等等小的json,逐一创建对应的模式,最后组合在一起验证完整的Json。
JsonSchema coordSchema = new JsonSchema();
coordSchema.Type = JsonSchemaType.Object;
coordSchema.Properties = new Dictionary<string, JsonSchema>
{
{ "lon", new JsonSchema { Type = JsonSchemaType.Float } },
{ "lat", new JsonSchema { Type = JsonSchemaType.Float } }
}; JsonSchema sysSchema = new JsonSchema();
sysSchema.Type = JsonSchemaType.Object;
sysSchema.Properties = new Dictionary<string, JsonSchema>
{
{ "type", new JsonSchema { Type = JsonSchemaType.Integer } },
{ "id", new JsonSchema { Type = JsonSchemaType.Integer } },
{ "message", new JsonSchema { Type = JsonSchemaType.Float } },
{ "country", new JsonSchema { Type = JsonSchemaType.String } },
{ "sunrise", new JsonSchema { Type = JsonSchemaType.Integer } },
{ "sunset", new JsonSchema { Type = JsonSchemaType.Integer } }
}; JsonSchema weatherItemSchema = new JsonSchema();
weatherItemSchema.Type = JsonSchemaType.Object;
weatherItemSchema.Properties = new Dictionary<string, JsonSchema>
{
{ "id", new JsonSchema { Type = JsonSchemaType.Integer } },
{ "main", new JsonSchema { Type = JsonSchemaType.String } },
{ "description", new JsonSchema { Type = JsonSchemaType.String } },
{ "icon", new JsonSchema { Type = JsonSchemaType.String } }
}; JsonSchema windSchema = new JsonSchema();
windSchema.Type = JsonSchemaType.Object;
windSchema.Properties = new Dictionary<string, JsonSchema>
{
{ "speed", new JsonSchema { Type = JsonSchemaType.Float } },
{ "deg", new JsonSchema { Type = JsonSchemaType.Integer } }
}; JsonSchema weatherSchema = new JsonSchema();
weatherSchema.Type = JsonSchemaType.Array;
weatherSchema.Items = new List<JsonSchema>();
weatherSchema.Items.Add(weatherItemSchema); JsonSchema mainSchema = new JsonSchema();
mainSchema.Type = JsonSchemaType.Object;
mainSchema.Properties = new Dictionary<string, JsonSchema>
{
{ "temp", new JsonSchema { Type = JsonSchemaType.Float } },
{ "pressure", new JsonSchema { Type = JsonSchemaType.Integer } },
{ "humidity", new JsonSchema { Type = JsonSchemaType.Integer } },
{ "temp_min", new JsonSchema { Type = JsonSchemaType.Float } },
{ "temp_max", new JsonSchema { Type = JsonSchemaType.Float } }
}; JsonSchema cloudsSchema = new JsonSchema();
cloudsSchema.Type = JsonSchemaType.Object;
cloudsSchema.Properties = new Dictionary<string, JsonSchema>
{
{ "all", new JsonSchema { Type = JsonSchemaType.Float } }
};
最后将这些分支的模式组合起来:
JsonSchema schema = new JsonSchema();
schema.Type = JsonSchemaType.Object;
schema.Properties = new Dictionary<string, JsonSchema>
{
{"coord", coordSchema },
{"sys", sysSchema },
{"weather",weatherSchema},
{"base", new JsonSchema{Type = JsonSchemaType.String} },
{"main",mainSchema},
{"wind",windSchema},
{"clouds",cloudsSchema},
{"dt", new JsonSchema{Type = JsonSchemaType.Integer} },
{"id", new JsonSchema{Type = JsonSchemaType.Integer} },
{"name", new JsonSchema{Type = JsonSchemaType.String} },
{"cod", new JsonSchema{Type = JsonSchemaType.Integer} }
};
bool valid = jobject.IsValid(schema);
也可以将最后的schema转化为字符串保存在专门的文件里,需要的时候从中读取:
{
"type": "object",
"properties": {
"coord": {
"type": "object",
"properties": {
"lon": {
"type": "number"
},
"lat": {
"type": "number"
}
}
},
"sys": {
"type": "object",
"properties": {
"type": {
"type": "integer"
},
"id": {
"type": "integer"
},
"message": {
"type": "number"
},
"country": {
"type": "string"
},
"sunrise": {
"type": "integer"
},
"sunset": {
"type": "integer"
}
}
},
"weather": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"main": {
"type": "string"
},
"description": {
"type": "string"
},
"icon": {
"type": "string"
}
}
}
},
"base": {
"type": "string"
},
"main": {
"type": "object",
"properties": {
"temp": {
"type": "number"
},
"pressure": {
"type": "integer"
},
"humidity": {
"type": "integer"
},
"temp_min": {
"type": "number"
},
"temp_max": {
"type": "number"
}
}
},
"wind": {
"type": "object",
"properties": {
"speed": {
"type": "number"
},
"deg": {
"type": "integer"
}
}
},
"clouds": {
"type": "object",
"properties": {
"all": {
"type": "number"
}
}
},
"dt": {
"type": "integer"
},
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"cod": {
"type": "integer"
}
}
}
验证说明:
1、原Json字符串,正确;
2、原Json的weather列表增加{"id": 801,"main": "Clouds","description": "few clouds","icon": "02d"},正确;
3、原Json的id修改为haha,错误;
4、原Json增加"planet":earth,错误,增加了额外的结构;
5、原Json删除"id": 2172797, "cod": 200,正确
Json.Net使用JSON Schema验证JSON格式【实例】的更多相关文章
- Json.Net使用JSON Schema验证JSON格式
Json.NET supports the JSON Schema standard via the JsonSchema and JsonValidatingReader classes. It s ...
- .Net使用JsonSchema验证Json
最近项目中遇到了这样的需求,需要对上传的Json进行验证,以确保Json数据的准确性.前后使用了两种方式来验证: (1)第一种方式的实现思想:根据Json数据的格式,严格定义相应的类结构,并在Syst ...
- 利用JSON Schema校验JSON数据格式
最近笔者在工作中需要监控一批http接口,并对返回的JSON数据进行校验.正好之前在某前端大神的分享中得知这个神器的存在,调研一番之后应用在该项目中,并取得了不错的效果,特地在此分享给各位读者. 什么 ...
- .net core json序列化首字符小写和日期格式处理
打开Startup.cs文件,在ConfigureServices方法中添加如下代码 public void ConfigureServices(IServiceCollection services ...
- JSON --- 一种轻量级的数据交换格式
目录 1. 语法 2. 解析与序列化 JSON.stringify( jsData[, filter, indent] ) JSON.parse( jsonData[, reduction]) JSO ...
- Go语言入门篇-jwt(json web token)权限验证
一.token.cookie.session的区别 1.cookie Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie. 内存Cookie由浏览器维护, ...
- C#如何Json转字符串;字符串转Json;Newtonsoft.Json(Json.Net)
Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库(下载地址http://json.codeplex.com/). 下面是Json序列化和反序列化的简单封装: /// & ...
- 黄聪:C#如何Json转字符串;字符串转Json;Newtonsoft.Json(Json.Net)学习笔记(转)
Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库(下载地址http://json.codeplex.com/). 下面是Json序列化和反序列化的简单封装: /// & ...
- c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具
c#实例化继承类,必须对被继承类的程序集做引用 0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...
随机推荐
- Artifacts Gallery - Seahorse Collector, The
Artifacts Gallery - Seahorse Collector, The
- asp.net 在线人数
很网站都有在线人数,这一功能无处不在.现在,我们就介绍在.NET中一个简单明了的方法来统计在线用户的多少,该方法的特点就是充分的利用了ASP.NET的特点,结合global.asax文件,用Appli ...
- 法线贴图——Normal Mapping
对于不曾学过.用过法线贴图的人来说,提到法线贴图,经常会提到的问题是什么是法线贴图?法线贴图用于解决什么问题?法线贴图的原理是什么?本文将就这三个问题阐述本人的一些见解,各位不喜勿喷!!! 谈到法线贴 ...
- 码农谷 球从M米高度自由下落第N次落地时反弹的高度
题目描述 一球从M米高度自由下落,每次落地后返回原高度的一半,再落下.它在第N次落地时反弹多高?共经过多少米? 保留两位小数. 输入描述 M N 输出描述 它在第N次落地时反弹多高?共经过多少米? 保 ...
- Apache无法访问 Forbidden
如图: 打开 找到: <Directory "E:/wamp/www/"> # # Possible values for the Options directive ...
- SE11
自省数据及表存储 2014年4月6日 21:37 类似JAVA反射的特性 SAP提供自省数据的机制 这样可以保证程序的灵活性和动态性 1.使用OO中的cl_abap_typed ...
- 仿AS语法来写HTML5—第1章,显示一张图片
最近开始学习html5,因为一直都是研究as,所以还是觉得as顺眼一点,但是html5也不能不学,于是就想出了,可以把html5用as的语法来写出来,做游戏应该来的比较顺手一些,下面开始第一篇 第一篇 ...
- java 求第n小的质数
package a小米; import java.util.Scanner; public class prime { public static boolean isPrime(int n){ fo ...
- MyEclipse中使用debug调试程序
最基本的操作是: 1.首先在一个java文件中设断点,然后debug as-->open debug Dialog,然后在对话框中选类后--> Run 当程序走到断 ...
- string,stringbuilder,stringbuffer
String可以储存和操作字符串,即包含多个字符的字符数据.这个String类提供了存储数值不可改变的字符串. StringBuilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义 ...