如何解决JSON Schema 条件结果
我正在尝试创建一个 JSON 模式验证器。我的 Json 架构验证某个属性值,并基于此将值分配给另一个属性。在我的 C# 代码中,我想根据它执行一个操作。如果您在我的架构中注意到,如果国家是“美利坚合众国”或“加拿大”,那么我将效果设置为“合规”,否则为“非投诉”。而且,在我的 C# 代码中,我需要“效果”的值,以便我可以做一些进一步的处理。那可能吗?如果没有,我的方法应该是什么?我是 Json Schema 的新手(我已经看到 Azure 策略做了类似的事情。)
这是我的架构
{
"type": "object","properties": {
"street_address": {
"type": "string"
},"country": {
"enum": [ "United States of America","Canada" ]
},"effect": {"type": "string"}
},"if": {
"properties": { "country": { "const": "United States of America" } }
},"then": {
"effect": "Compliant"
},"else": {
"effect": "Non-Compliant"
}
}
这是我的文档
{
"properties": {
"street_address": "1600 Pennsylvania Avenue NW","country": "Canada","postal_code": "20500"
}
}
这是我的 C# 代码
JObject data = null;
var currentDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
using (StreamReader r = new StreamReader(currentDirectory + @"/Documents/document.json"))
using (JsonTextReader reader = new JsonTextReader(r))
{
data = (JObject)JToken.ReadFrom(reader);
}
JsonSchema schema = JsonSchema.Parse(File.ReadAllText(currentDirectory + @"/Schemas/Schema.json"));
IList<string> messages;
var properties = (JObject)data["properties"];
bool valid = properties.IsValid(schema,out messages);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。