问题描述
我正在尝试创建一个 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 (将#修改为@)