反序列化JSON数据

时间:2019-04-01 20:11:44

标签: c# json json.net

我目前正在尝试解析一些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字段的数据未正确提取。任何帮助/建议将不胜感激。

4 个答案:

答案 0 :(得分:0)

您缺少课程。将此类添加到您的模型中:

public class MessageArgument
{
    public SignUpModal signUpModal { get; set; }
}

然后在Parameter类中更改此行:

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

对此:

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

答案 1 :(得分:0)

合同不匹配。您应该将JSON更改为:

{
    "messageFormat": "JSON",
    "messageArguments": [
    {
        "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<MessageArgument> messageArguments { get; set; }
}

class MessageArgument {
    public SignUpModal SignUpModal {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; }
}

答案 2 :(得分:0)

正如马特·伯兰德(Matt Burland)指出的那样,问题出在messageArguments上。 如果您还使用罗伯特·哈维(Robert Harvey)提供的资源,那么您还会发现所创建的对象结构有所不同。

Parameter应该由

组成
public string messageFormat { get; set; }
public List<MessageArgument> messageArguments { get; set; }

您还将需要

{
    public SignUpModal signUpModal { get; set; }
}

答案 3 :(得分:0)

您似乎使用了错误的类生成器。我已将您的json复制到jsonutils,它给出了这个模型:

public class SignUpModal
{
    public int Id { get; set; }
    public object FirstName { get; set; }
    public object LastName { get; set; }
    public object UserName { get; set; }
    public string Email { get; set; }
    public object RoleId { get; set; }
    public string Password { get; set; }
    public object IsActive { get; set; }
    public object SecretKey { get; set; }
    public object Token { get; set; }
    public object Role { get; set; }
    public object RolePermissions { get; set; }
    public object ImagePath { get; set; }
    public object CurrentDate { get; set; }
}

public class MessageArgument
{
    [JsonProperty("signUpModal")]
    public SignUpModal SignUpModal { get; set; }
}

public class Parameter
{
    [JsonProperty("messageFormat")]
    public string MessageFormat { get; set; }

    [JsonProperty("messageArguments")]
    public IList<MessageArgument> MessageArguments { get; set; }
}

请注意,此代码生成器允许您通过添加JsonProperty属性来拥有PascalCase属性

现在,如果将输入反序列化,您将看到非空的EmailPassword

var input = "{\r\n  \"messageFormat\": \"JSON\",\r\n  \"messageArguments\": [\r\n    {\r\n      \"signUpModal\": {\r\n        \"Id\": 0,\r\n        \"FirstName\": null,\r\n        \"LastName\": null,\r\n        \"UserName\": null,\r\n        \"Email\": \"<email address>\",\r\n        \"RoleId\": null,\r\n        \"Password\": \"<password>\",\r\n        \"IsActive\": null,\r\n        \"SecretKey\": null,\r\n        \"Token\": null,\r\n        \"Role\": null,\r\n        \"RolePermissions\": null,\r\n        \"ImagePath\": null,\r\n        \"CurrentDate\": null\r\n      }\r\n    }\r\n  ]\r\n}";
var result = JsonConvert.DeserializeObject<Parameter>(input);

Console.WriteLine(result.MessageArguments.First().SignUpModal.Email); // will print "<email address>"