我想序列化这个类:
public class CarDisplay { public string Name { get; set; } public string Brand { get; set; } public string Year { get; set; } public PictureDisplay[] Pictures { get; set; } } public class PictureDisplay { public int Id { get; set; } public string SecretKey { get; set; } public string AltText { get; set; } }
对于这个Json测试:
{名称:"名称值",品牌:"品牌价值",年份:"年份价值",图片:["url1","url2","url3"]}
请注意,每个Car都有一个只包含url字符串的图片数组,而不是Picture类具有的所有属性.
我知道Json.NET有自定义解析器的概念,但我不确定如何使用它.
public class PictureDisplayConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { // convert array of picture to json string; } public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer) { // convert back json string into array of picture } public override bool CanConvert(Type objectType) { return true; } }
在你的车展中:
public class CarDisplay { public string Name { get; set; } public string Brand { get; set; } public string Year { get; set; } [JsonConverter(typeof(PictureDisplayConverter ))] public PictureDisplay[] Pictures { get; set; } }