给出一个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,正确