![](/img/trans.png)
[英]How to serialize a class with a complex property using XML serialization in .NET?
[英]how to serialize only specific value from complex type property of class using newton soft
我有一個 class 具有多個屬性,其中一些屬性是一種復雜類型,它本身具有多個其他屬性,我不想在所有這些不需要的屬性上使用JsonIgnore
注釋,尋找僅獲取一個屬性以從某些屬性中序列化的方法復雜類型。
例如我有 3 節課
class Organization
{
public Int32 ID {get; set;}
public string Name {get; set;}
public Geography Location {get; set;}
public Employee AreaHead {get; set;}
}
class Geography
{
public Int32 GeoID {get; set;}
public string Country {get; set;}
public string City {get; set;}
public string State {get; set;}
}
class Employee
{
public Int32 EmpID {get; set;}
public string Name {get; set;}
public DateTime DOB {get; set;}
public string Gender {get; set;}
}
here I want to serialize the Organization
class object, that should include only 'Country' from Geography and 'EmpID' from Employee for respective Location and AreaHead properties, the json string output I am expecting as below using JsonConvert.SerializeObject
method from NewtonSoft library.
{
"ID":1,
"Name":"Sales",
"Location":"India",
"AreaHead":5464
}
這可以使用 DefaultContractResolver 嗎?
合約解析器
public class DynamicContractResolver : DefaultContractResolver
{
private readonly InfoProperty[] _jasonProperies;
public DynamicContractResolver(params InfoProperty[] includePropertyForSerilization)
{
_jasonProperies = includePropertyForSerilization;
}
protected override JsonContract CreateContract(Type objectType)
{
if (typeof(TFDisplay).IsAssignableFrom(objectType))
{
var contract = this.CreateObjectContract(objectType);
contract.Converter = null; // Also null out the converter to prevent infinite recursion.
return contract;
}
return base.CreateContract(objectType);
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
if (_jasonProperies != null && _jasonProperies.Length > 0)
{
if (_jasonProperies.Any(o => (o.PropertyContainer ?? type) == type))
{
properties = properties.Where(p => _jasonProperies.Any(o => (o.PropertyContainer ?? type) == type && o.PropertyName == p.PropertyName)).ToList();
}
}
return properties;
}
public class InfoProperty
{
public InfoProperty(string propertyName)
{
this.PropertyName = propertyName;
}
public InfoProperty() { }
public InfoProperty(Type propertyContainer, string propertyName)
{
this.PropertyContainer = propertyContainer;
this.PropertyName = propertyName;
}
public Type PropertyContainer { get; set; }
public string PropertyName { get; set; }
}
}
You can create an anonymous object with only required properties from the given Organization
class and use JsonConvert.SerializeObject()
function to get the desired JSON string
從現有的組織實例創建一個匿名類型。
var input = new { ID = organization.ID, Name = organization.Name, Location = organization.Location.Country, AreaHead = organization.AreaHead.EmpID }
現在使用 NewtonSoft.Json 庫序列化這個 object ,
using Newtonsoft.Json; ... var result = JsonConvert.SerializeObject(input, Formatting.Indented); Console.WriteLine(result);
使用JObject.FromObject()
方法,
//Here input is an anonymous object variable declared in above section
var result = JObject.FromObject(input).ToString()
Console.WriteLine(result);
在線試用: .NET FIDDLE
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.