我想动态更改json属性名并序列化对象。
这是我的两个不同实体的json
对于客户
{
"userName": "66666",
"password": "test1234",
"customersList": [
{
"address": "Test Stree2",
"customerNumber": "US01-000281",
"city": ""
}
]
}
联系
{
"userName": "66666",
"password": "test1234",
"contactList": [
{
"address": "Test stree1",
"contactNumber": "US01-000281",
"city": ""
}
]
}
并且持有此数据的模型如下
public class URequest<T>
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string userName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string password { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public IList<T> requestList { get; set; }
}
上面的代码requestList
中的可能包含contacts
或customer
的列表,但在发送时我想将requestList
json属性名称更改为相应的实体名称,即{{ 1}}它将是customer
,而对于customerList
,序列化后它将是contact
。
答案 0 :(得分:3)
使用ContentResolver
我解决了问题
这是代码
public class UserRequestResolver : DefaultContractResolver
{
private string propertyName;
public UserRequestResolver()
{
}
public UserRequestResolver(String name)
{
propertyName = name;
}
public new static readonly UserRequestResolver Instance = new UserRequestResolver();
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
if ( property.PropertyName == "requestList")
{
property.PropertyName = propertyName;
}
return property;
}
}
一旦可以在构造函数中传递特定的属性名称。
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new UserRequestResolver("contactList");
答案 1 :(得分:3)
您可以创建自定义JsonConverter。
Using custom JsonConverter in order to alter the serialization of the portion of an object
实施例
public class Customer
{
public string Name { get; set; }
}
public class Client
{
public string Name { get; set; }
}
public class URequest<T>
{
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string userName { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string password { get; set; }
[JsonIgnore]
public IList<T> requestList { get; set; }
}
public class URequestConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(URequest<T>));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var objectType = value.GetType().GetGenericArguments()[0];
URequest<T> typedValue = (URequest<T>) value;
JObject containerObj = JObject.FromObject(value);
containerObj.Add($"{objectType.Name.ToLower()}List", JToken.FromObject(typedValue.requestList));
containerObj.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
您可以像这样使用
[TestMethod]
public void TestMethod1()
{
URequest<Customer> request = new URequest<Customer>();
request.password = "test";
request.userName = "user";
request.requestList = new List<Customer>();
request.requestList.Add(new Customer() { Name = "customer" });
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
settings.Converters.Add(new URequestConverter<Customer>());
Console.WriteLine(JsonConvert.SerializeObject(request, settings));
}