简体   繁体   English

将 json 反序列化为新对象

[英]Deserializing json into new objects

I'm reading some Json from an api which I don't own, and therefore, I can't change the response.我正在从我不拥有的 api 中读取一些 Json,因此,我无法更改响应。 When I'm deserializing the Json into C# objects using Newtonsoft, I'm running into a problem with the 'ChangedAttributes' field.当我使用 Newtonsoft 将 Json 反序列化为 C# 对象时,我遇到了“ChangedAttributes”字段的问题。 When creating these classes, Visual Studio wants to create a class for 'Sex', and one for 'MaritalStatus', but they aren't being populated.创建这些类时,Visual Studio 想要为“Sex”创建一个类,并为“MaritalStatus”创建一个类,但它们没有被填充。 Furthermore, the list of ChangedAttributes can be any number of hundreds of different possibilities.此外,ChangedAttributes 列表可以是数百种不同的可能性。 So, I'd like to deserialize those attributes into a generic type class, say, something like:所以,我想将这些属性反序列化为一个泛型类,比如:

public class Attribute
{

public string Name {get;set;}
public string Old {get;set;}
public string New {get;set;}
}

Can someone tell me how I might accomplish that using Newtonsoft?有人可以告诉我如何使用 Newtonsoft 实现这一目标吗? Here's a sample of the Json that I'm trying to parse:这是我试图解析的 Json 示例:

{
  "Context" : [ {
    "PersonName" : "Smith, Bob",
    "PersonNumber" : "001246",
    "PrimaryPhoneNumber" : "",
    "PersonId" : "300000009560451",
    "WorkerType" : "EMP",
    "PeriodType" : "E",
    "DMLOperation" : "UPDATE",
    "EffectiveStartDate" : "2004-08-27",
    "EffectiveDate" : "2004-08-27"
  } ],
  "Changed Attributes" : [ {
    "Sex" : {
      "old" : null,
      "new" : "M"
    }
  }, {
    "MaritalStatus" : {
      "old" : null,
      "new" : "S"
    }
  } ]
}

Here are the C# classes that VS generated for me:以下是 VS 为我生成的 C# 类:

public class AtomFeed_Empupdate
{
    public AtomFeed_Empupdate_Context[] Context { get; set; }
    public AtomFeed_Empupdate_ChangedAttribute[] ChangedAttributes { get; set; }
}

public class AtomFeed_Empupdate_Context
{
    public string PersonName { get; set; }
    public string PersonNumber { get; set; }
    public string WorkEmail { get; set; }
    public string PrimaryPhoneNumber { get; set; }
    public string PersonId { get; set; }
    public string WorkerType { get; set; }
    public string PeriodType { get; set; }
    public string DMLOperation { get; set; }
    public string EffectiveStartDate { get; set; }
    public string EffectiveDate { get; set; }
}

public class AtomFeed_Empupdate_ChangedAttribute
{
    public AtomFeed_Empupdate_Sex Sex { get; set; }
    public AtomFeed_Empupdate_Maritalstatus MaritalStatus { get; set; }
}

public class AtomFeed_Empupdate_Sex
{
    public string old { get; set; }
    public string _new { get; set; }
}

public class AtomFeed_Empupdate_Maritalstatus
{
    public string old { get; set; }
    public string _new { get; set; }
}

And here is how I am deserializing the Json:这是我反序列化 Json 的方式:

AtomFeed_Empupdate eu = Newtonsoft.Json.JsonConvert.DeserializeObject<AtomFeed_Empupdate>(json);

Thanks for your help!谢谢你的帮助!

You may change Attribute class a little bit and decorate it with JsonProperty attribute with proper names您可以稍微更改Attribute类并使用具有正确名称的JsonProperty属性对其进行装饰

public class Attribute
{
    [JsonProperty("old")]
    public string Old { get; set; }
    [JsonProperty("new")]
    public string New { get; set; }
}

And use Dictionary<string, Attribute>[] (since you have an array of key-value pairs) for Changed Attributes JSON token并使用Dictionary<string, Attribute>[] (因为你有一个键值对数组)作为Changed Attributes JSON 令牌

public class AtomFeed_Empupdate
{
    public AtomFeed_Empupdate_Context[] Context { get; set; }
    [JsonProperty("Changed Attributes")]
    public Dictionary<string, Attribute>[] ChangedAttributes { get; set; }
}

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

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