C# Newtonsoft反序列化以对象存储底层JSON作为属性
我正在使用Newtonsoft将JSON从REST调用反序列化到C#对象。对象是一个人员列表。这个人有很多财产,但现在我只储存其中的一部分。我希望在包含构成整个人的JSON的Person上有一个string属性。有办法做到这一点吗?我现在正在将它写回SQL数据库,我不需要这些值,但如果需要的话,我希望将来可以使用它 对象类C# Newtonsoft反序列化以对象存储底层JSON作为属性,c#,json,json.net,C#,Json,Json.net,我正在使用Newtonsoft将JSON从REST调用反序列化到C#对象。对象是一个人员列表。这个人有很多财产,但现在我只储存其中的一部分。我希望在包含构成整个人的JSON的Person上有一个string属性。有办法做到这一点吗?我现在正在将它写回SQL数据库,我不需要这些值,但如果需要的话,我希望将来可以使用它 对象类 public class Worker { public string associateOID { get; set; } public WorkerID
public class Worker
{
public string associateOID { get; set; }
public WorkerID workerID { get; set; }
public Person person { get; set; }
public WorkerDates workerDates { get; set; }
public WorkerStatus workerStatus { get; set; }
public List<WorkAssignment> workAssignments { get; set; }
public CustomFieldGroup customFieldGroup { get; set; }
public BusinessCommunication businessCommunication { get; set; }
public string JSON { get; set; }
}
public class Meta
{
public int totalNumber { get; set; }
}
public class WorkerResult
{
public List<Worker> workers { get; set; }
public Meta meta { get; set; }
}
公共类工作者
{
公共字符串associateOID{get;set;}
公共工作ID工作ID{get;set;}
公众人物{get;set;}
公共工作日期工作日期{get;set;}
公共工作状态工作状态{get;set;}
公共列表工作分配{get;set;}
公共CustomFieldGroup CustomFieldGroup{get;set;}
公共业务通信业务通信{get;set;}
公共字符串JSON{get;set;}
}
公共类元
{
公共整数totalNumber{get;set;}
}
公共类WorkerResult
{
公共列表工作程序{get;set;}
公共元{get;set;}
}
我现有的反序列化调用:
WorkerResult result = JsonConvert.DeserializeObject<WorkerResult>(json);
WorkerResult result=JsonConvert.DeserializeObject(json);
将JsonIgnore属性添加到JSON属性,如下所示:
[JsonIgnore()]
public string JSON { get; set; }
在反序列化对象后,可以使用“执行此操作”
JObject workerResult = JObject.Parse(json);
// this should contain a list of all the workers
IList<JToken> workers = workerResult["workers"].Children().ToList();
将JsonIgnore属性添加到JSON属性,如下所示:
[JsonIgnore()]
public string JSON { get; set; }
在反序列化对象后,可以使用“执行此操作”
JObject workerResult = JObject.Parse(json);
// this should contain a list of all the workers
IList<JToken> workers = workerResult["workers"].Children().ToList();
我认为您的意思是希望将JSON中的所有属性存储到c#对象中,以便以后需要时可以访问它们 为此,请使用
[JsonExtensionData]
注释
public class Worker
{
public string associateOID { get; set; }
// Any other attributes that you want to use as .NET types go here
// all attributes that don't have a property will be put into this dictionary
// either as primitive types, or as objects of type Newtonsoft.Json.Linq.JObject
// supports nested objects of any Json structure, and will serialize correctly.
[JsonExtensionData]
public Dictionary<string,object> ExtraAttributes {get;set;}
}
我认为您的意思是希望将JSON中的所有属性存储到c#对象中,以便以后需要时可以访问它们 为此,请使用
[JsonExtensionData]
注释
public class Worker
{
public string associateOID { get; set; }
// Any other attributes that you want to use as .NET types go here
// all attributes that don't have a property will be put into this dictionary
// either as primitive types, or as objects of type Newtonsoft.Json.Linq.JObject
// supports nested objects of any Json structure, and will serialize correctly.
[JsonExtensionData]
public Dictionary<string,object> ExtraAttributes {get;set;}
}
当然有可能。你有什么问题?您是否编写了任何代码?我更新了我的原始版本,以包含对象类和反序列化的代码。我真的不知道从那里去哪里。没有看到任何属性可以执行此操作。对于NewtonSoft,这是直接的。只需将类的
JSON
属性设置为JsonConvert.SerializeObject
。在我的Worker类中,我只从JSON中获取一些属性,但希望存储所有JSON和数据库的属性。使用自定义的JsonConverter
可以实现,但会更好地满足您的需要吗?请参阅和。当然这是可能的。你有什么问题?您是否编写了任何代码?我更新了我的原始版本,以包含对象类和反序列化的代码。我真的不知道从那里去哪里。没有看到任何属性可以执行此操作。对于NewtonSoft,这是直接的。只需将类的JSON
属性设置为JsonConvert.SerializeObject
。在我的Worker类中,我只从JSON中获取一些属性,但希望存储所有JSON和数据库的属性。使用自定义的JsonConverter
可以实现,但会更好地满足您的需要吗?请参阅和。JSON位于列表中的2层对象中。@JasonWebber我已更新我的答案以匹配您的对象JSON位于列表中的2层对象中。@JasonWebber我已更新我的答案以匹配您的对象