简体   繁体   English

使用Json.Net(NewtonSoft)在C#中反序列化json

[英]Deserialize a json in C# with Json.Net (NewtonSoft)

I'm using Newtonsoft.Json (v6.0.0.0) and I'm trying to deserialize a JSON into an enumeration. 我正在使用Newtonsoft.Json(v6.0.0.0),并且试图将JSON反序列化为枚举。

I tried to use the attribute EnumMember , wich works for the serialization (send a JSON from a .Net Web API) but does not seem to work for the deserialization. 我尝试使用属性EnumMember ,该方法可用于序列化(从.Net Web API发送JSON),但似乎不适用于反序列化。

For begining, I have a model like this: 首先,我有一个像这样的模型:

[JsonObject]
public class MyModel
{
    /// <summary>
    /// Gets or sets the username
    /// </summary>
    [Display(Name = @"UserName")]
    [JsonProperty(PropertyName = "userName", Required = Required.Always)]
    public string UserName { get; set; }

          /* .
             . REST OF THE MODEL
             .
             . */

    /// <summary>
    /// Gets or sets the enum model.
    /// </summary>
    [Display(Name = @"EnumModel")]
    [JsonProperty(PropertyName = "enummodel", Required = Required.Always)]
    public MyEnumModel EnumModel { get; set; }
}

MyEnumModel is an enum with those values: MyEnumModel是具有这些值的枚举:

[JsonConverter(typeof(StringEnumConverter))]
public enum MyEnumModel
{
    [EnumMember(Value = "My Value 1")]
    MyValueOne,

    [EnumMember(Value = "My Value 2")]
    MyValueTwo,

    [EnumMember(Value = "My Value 3")]
    MyValueThree
}

When I serialize this enum, I get the correct value, defined by the attribute EnumMember . 序列化此枚举时,将获得由属性EnumMember定义的正确值。 So in the result of the call, I have a JSON with value "My Value 1" or "My Value 2" or "My Value 3". 因此,在调用结果中,我有一个值为“我的值1”或“我的值2”或“我的值3”的JSON。

But when I send back the JSON with the same value ("My Value 1" for example), I have this error message: 但是,当我以相同的值(例如,“我的值1”)发送回JSON时,会出现以下错误消息:

"The value 'My Value 1' is not valid for EnumModel." 

It seems to not use the EnumMember attribute for the deserialization because when I send the value "MyValueOne", it works. 它似乎不使用EnumMember属性进行反序列化,因为当我发送值“ MyValueOne”时,它可以工作。

What did I miss? 我错过了什么? How the attribute [EnumMember()] really works when I deserialize? 反序列化时[EnumMember()]属性如何真正起作用?

Thanks a lot for the help! 非常感谢您的帮助!

EDIT: JSON sample 编辑:JSON示例

{  
   "userName":"testuser@gmail.com",
   "enummodel":"My Value 1"
}

EDIT 2: Model validation 编辑2:模型验证

Sorry, but I forgot to mention one important thing. 抱歉,但是我忘了提到一件重要的事情。 I'm using, in my controller a ModelState validation: 我在控制器中使用ModelState验证:

if (!this.ModelState.IsValid)
{
    return this.BadRequest(this.ModelState);
}

I think the json is correctly deserialize with attributes EnumMember but the validation failed because the validator do not use the EnumMember to validate my model? 我认为json使用属性EnumMember正确反序列化,但是验证失败,因为验证程序未使用EnumMember验证我的模型? Is it correct? 这是正确的吗?

My problem is in the ModelState validation and not the deserialization. 我的问题在于ModelState验证,而不是反序列化。 With the EnumMember attribute, everything works fine. 使用EnumMember属性,一切正常。

Apparently the ModelState validation does not seem to read EnumMember attributes while validating. 显然,在验证时,ModelState验证似乎没有读取EnumMember属性。 But this is an other subject :). 但这是另一个主题:)。

Thanks for the help. 谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM