Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JSON转换为C#字典?_C#_Javascript_Json_Asp.net Mvc 3_Dictionary - Fatal编程技术网

如何将JSON转换为C#字典?

如何将JSON转换为C#字典?,c#,javascript,json,asp.net-mvc-3,dictionary,C#,Javascript,Json,Asp.net Mvc 3,Dictionary,我有以下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': /^[^\

我有以下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."
        }
    }
};
var validate=新字典{
{
@“(\.org\.info\.biz\.name)$”,
新的{
Type=“size”,
模式=@“^.{3,64}$”,
Message=“您的域最多可以包含26个字符,至少可以包含3个。”
}
}
};
其中,动态对象的键是域扩展的regex模式,regex inside
模式
键是应该匹配域名的键

但是我不知道如何在
字典
动态
部分中放置2个验证类型

以前有人做过类似的事情吗?或者这很愚蠢,我应该换一种方式做


这样做的意义在于,我可以将字典序列化为Json。

如果您试图为MVC站点这样做,我认为您应该研究一下,让运行时为您处理管道。这篇文章讨论了如何实现自定义RegularExpressionAttribute,如下所示。

我在字典的动态部分尝试了
列表:

var validate = new Dictionary<string, List<dynamic>> {
    {
        "(\\.org|\\.info|\\.biz|\\.name)$",
        new List<dynamic>
        {
            new {
                Type = "size",
                Pattern = @"^.{3,64}$",
                Message = "Your domain can have at max 26 characters and at least 3."
            }
        }
    },
    {
        ".*",
        new List<dynamic>
        {
            new {
                Type = "general",
                Pattern = @"^[^\.-].*[^\.-]$",
                Message = "Your domain name shouldn\'t contain . or - at the beginning or the end."
            },
            new {
                Type = "characters",
                Pattern = @"^[abcdefghijklmnopqrstwuvxyz0123456789]+$",
                Message = "Your domain name should contain only alphanumeric characters."
            }
        }
    }
};
var validate=新字典{
{
“(\\.org\\.info\\.biz\\.name)$”,
新名单
{
新的{
Type=“size”,
模式=@“^.{3,64}$”,
Message=“您的域最多可以包含26个字符,至少可以包含3个。”
}
}
},
{
".*",
新名单
{
新的{
Type=“一般”,
模式=@“^[^\.-].[^\.-]$”,
Message=“您的域名不应包含。或-在开头或结尾。”
},
新的{
Type=“characters”,
模式=@“^[abcdefghijklmnopqrstwuvxyz0123456789]+$”,
Message=“您的域名应仅包含字母数字字符。”
}
}
}
};

使用
Json
来自
JsonResult
mvc3视图的
Json,它返回了我需要的Json。

你想做什么?我正试图用它创建一个自定义数据注释,然后我想用asp-net-mvc3将字典中的内容作为Json发送到视图,这样我就可以在C#代码和每次我们更改C#验证时,客户端都会跟随。我对自定义验证非常敏锐,特别是如果它是mvc3,不管你的链接是从mvc2来的,我认为它们没有太大的差异。当将正则表达式验证作为序列化Json发送到视图时,这个问题无法解决我的问题。看起来您正在尝试验证正则表达式,不是吗?我正在尝试使用正则表达式进行验证,但我需要在字典中进行验证,因为我想在之后对其进行迭代。您也可以使用字典这也是一个很好的解决方案。