反序列化 JSON 数据

我目前正在尝试解析一些 JSON 数据。这是我得到的 JSON 字符串的格式:


{

  "messageFormat": "JSON",

  "messageArguments": [

    {

      "signUpModal": {

        "Id": 0,

        "FirstName": null,

        "LastName": null,

        "UserName": null,

        "Email": "<email address>",

        "RoleId": null,

        "Password": "<password>",

        "IsActive": null,

        "SecretKey": null,

        "Token": null,

        "Role": null,

        "RolePermissions": null,

        "ImagePath": null,

        "CurrentDate": null

      }

    }

  ]

}

我创建了两个类来匹配这个数据结构:


public class Parameter

{

    public string messageFormat { get; set; }

    public List<SignUpModal> messageArguments { get; set; }

}


public class SignUpModal

{

    public int? Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string UserName { get; set; }

    public string Email { get; set; }

    public int? RoleId { get; set; }

    public string Password { get; set; }

    public bool? IsActive { get; set; }

    public int? SecretKey { get; set; }

    public string Token { get; set; }

    public string Role { get; set; }

    public string RolePermissions { get; set; }

    public string ImagePath { get; set; }

    public string CurrentDate { get; set; }

}

现在,当我使用时var param = JsonConvert.DeserializeObject<Parameter>(tempData);没有错误,并且MessageFormat提交的文件正确显示“JSON”。该MessageArguments字段还包含该类的一个实例SignUpModal,但是该类中的所有字段SignUpModal都是null.


我试过以各种方式修改类结构,但数据甚至不会反序列化。我不确定为什么messageArguments没有正确提取该字段的数据。任何帮助/建议将不胜感激。


侃侃无极
浏览 121回答 4
4回答

手掌心

合同不匹配。您应该将 JSON 更改为:{&nbsp; &nbsp; "messageFormat": "JSON",&nbsp; &nbsp; "messageArguments": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "Id": 0,&nbsp; &nbsp; &nbsp; &nbsp; "FirstName": null,&nbsp; &nbsp; &nbsp; &nbsp; "LastName": null,&nbsp; &nbsp; &nbsp; &nbsp; "UserName": null,&nbsp; &nbsp; &nbsp; &nbsp; "Email": "<email address>",&nbsp; &nbsp; &nbsp; &nbsp; "RoleId": null,&nbsp; &nbsp; &nbsp; &nbsp; "Password": "<password>",&nbsp; &nbsp; &nbsp; &nbsp; "IsActive": null,&nbsp; &nbsp; &nbsp; &nbsp; "SecretKey": null,&nbsp; &nbsp; &nbsp; &nbsp; "Token": null,&nbsp; &nbsp; &nbsp; &nbsp; "Role": null,&nbsp; &nbsp; &nbsp; &nbsp; "RolePermissions": null,&nbsp; &nbsp; &nbsp; &nbsp; "ImagePath": null,&nbsp; &nbsp; &nbsp; &nbsp; "CurrentDate": null&nbsp; &nbsp; }]}或者改变实体public class Parameter{&nbsp; &nbsp; public string messageFormat { get; set; }&nbsp; &nbsp; public List<MessageArgument> messageArguments { get; set; }}class MessageArgument {&nbsp; &nbsp; public SignUpModal SignUpModal {get; set;}}public class SignUpModal{&nbsp; &nbsp; public int? Id { get; set; }&nbsp; &nbsp; public string FirstName { get; set; }&nbsp; &nbsp; public string LastName { get; set; }&nbsp; &nbsp; public string UserName { get; set; }&nbsp; &nbsp; public string Email { get; set; }&nbsp; &nbsp; public int? RoleId { get; set; }&nbsp; &nbsp; public string Password { get; set; }&nbsp; &nbsp; public bool? IsActive { get; set; }&nbsp; &nbsp; public int? SecretKey { get; set; }&nbsp; &nbsp; public string Token { get; set; }&nbsp; &nbsp; public string Role { get; set; }&nbsp; &nbsp; public string RolePermissions { get; set; }&nbsp; &nbsp; public string ImagePath { get; set; }&nbsp; &nbsp; public string CurrentDate { get; set; }}

忽然笑

你错过了一节课。将此类添加到您的模型中:public class MessageArgument{&nbsp; &nbsp; public SignUpModal signUpModal { get; set; }}然后在类中更改这一行Parameter:public List<SignUpModal> messageArguments { get; set; }对此:public List<MessageArgument> messageArguments { get; set; }

一只萌萌小番薯

看起来您使用了错误的类生成器。我已将您的 json 复制到jsonutils,它为我提供了这个模型:public class SignUpModal{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public object FirstName { get; set; }&nbsp; &nbsp; public object LastName { get; set; }&nbsp; &nbsp; public object UserName { get; set; }&nbsp; &nbsp; public string Email { get; set; }&nbsp; &nbsp; public object RoleId { get; set; }&nbsp; &nbsp; public string Password { get; set; }&nbsp; &nbsp; public object IsActive { get; set; }&nbsp; &nbsp; public object SecretKey { get; set; }&nbsp; &nbsp; public object Token { get; set; }&nbsp; &nbsp; public object Role { get; set; }&nbsp; &nbsp; public object RolePermissions { get; set; }&nbsp; &nbsp; public object ImagePath { get; set; }&nbsp; &nbsp; public object CurrentDate { get; set; }}public class MessageArgument{&nbsp; &nbsp; [JsonProperty("signUpModal")]&nbsp; &nbsp; public SignUpModal SignUpModal { get; set; }}public class Parameter{&nbsp; &nbsp; [JsonProperty("messageFormat")]&nbsp; &nbsp; public string MessageFormat { get; set; }&nbsp; &nbsp; [JsonProperty("messageArguments")]&nbsp; &nbsp; public IList<MessageArgument> MessageArguments { get; set; }}请注意,此代码生成器允许您通过添加JsonProperty属性为属性设置 PascalCase现在,如果您反序列化您的输入,您将看到非空Email和Password:var input = "{\r\n&nbsp; \"messageFormat\": \"JSON\",\r\n&nbsp; \"messageArguments\": [\r\n&nbsp; &nbsp; {\r\n&nbsp; &nbsp; &nbsp; \"signUpModal\": {\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"Id\": 0,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"FirstName\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"LastName\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"UserName\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"Email\": \"<email address>\",\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"RoleId\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"Password\": \"<password>\",\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"IsActive\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"SecretKey\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"Token\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"Role\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"RolePermissions\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"ImagePath\": null,\r\n&nbsp; &nbsp; &nbsp; &nbsp; \"CurrentDate\": null\r\n&nbsp; &nbsp; &nbsp; }\r\n&nbsp; &nbsp; }\r\n&nbsp; ]\r\n}";var result = JsonConvert.DeserializeObject<Parameter>(input);Console.WriteLine(result.MessageArguments.First().SignUpModal.Email); // will print "<email address>"

翻翻过去那场雪

正如 Matt Burland 指出的那样,问题在于messageArguments. 如果您还使用 Robert Harvey 提供的资源,您还会发现所创建的对象结构有所不同。Parameter应该由public string messageFormat { get; set; }public List<MessageArgument> messageArguments { get; set; }你还需要一个{&nbsp; &nbsp; public SignUpModal signUpModal { get; set; }}
打开App,查看更多内容
随时随地看视频慕课网APP