我有以下 JSON:
validate = {
'(\\.org|\\.info|\\.biz|\\.name)$': [
{ 'type': 'size', 'pattern': /^.{3,64}$/, 'error': 'Your domain can have at max 26 characters and at least 3.' }
],
'.*': [
{ 'type': 'general', 'pattern': /^[^\.-].*[^\.-]$/, 'message': 'Your domain name shouldn\'t contain . or - at the beginning or the end.' },
{ 'type': 'characters', 'pattern': /^[abcdefghijklmnopqrstwuvxyz0123456789]+$/, 'error': 'Your domain can have at max 26 characters and at least 3.' }
]
};
并尝试像这样使用:
var validate = new Dictionary<string, dynamic> {
{
@"(\.org|\.info|\.biz|\.name)$",
new {
Type = "size",
Pattern = @"^.{3,64}$",
Message = "Your domain can have at max 26 characters and at least 3."
}
}
};
动态对象的键是域扩展的正则表达式模式,Pattern
键中的正则表达式是应该匹配域名的键。
但我不知道如何将 2 个验证类型放入 Dictionary
的 dynamic
部分。
有没有人以前做过类似的事情,或者这很愚蠢,我应该换一种方式做?
这样做的目的是我可以将字典序列化为 Json。
最佳答案
如果您尝试为 MVC 站点执行此操作,我认为您应该查看 custom validation ,并让运行时为您处理管道。这question其中讨论了如何实现自定义 RegularExpressionAttribute,它出现在这里。
关于c# - 如何将 JSON 转换为 C# 字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957170/