3

我想把一个扁平的json字符串变成一个模型,目标类有子类,扁平的json有所有带前缀的子类对象;像“{classname}.{property}”。

{
    "FirstName": "Joey",
    "LastName": "Billy",
    "EmploymentDetails.JobTitle": "JobTitle",
    "EmploymentDetails.StartDate": "2015-01-01T00:00:00",
    "ContactDetails.HouseNumberName": "10",
    "ContactDetails.Road": "Road"
}

这是我的目标课程:

public class Person {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual EmploymentDetails EmploymentDetails { get;set;}
    public virtual ContactDetails ContactDetails { get;set;}
}
public class EmploymentDetails {
    public string JobTitle { get; set; }
    public DateTime StartDate { get; set; }
}
public class ContactDetails {
    public string HouseNumberName { get; set; }
    public string Road { get; set; }
}

我尝试了以下方法:

public static void main() {
    var json = @"{""FirstName"": ""Joey"",""LastName"": ""Billy"",""EmploymentDetails.JobTitle"": ""JobTitle"",""EmploymentDetails.StartDate"": ""2015-01-01T00:00:00"",""ContactDetails.HouseNumberName"": ""10"",""ContactDetails.Road"": ""Road"",}";

    //try using AutoMapper
    Mapper.CreateMap<string,Person>();
    var personModel = Mapper.Map<Person>(json);
    //just returns null values

    //try using Newtonsoft
    personModel = Newtonsoft.Json.JsonConvert.DeserializeObject<Person>(json);
    //fills values but obviously returns no nested data
}

我知道 Automapper 有 RecognizePrefix 和 RecognizeDestinationPrefix,但 AutoMapper 似乎只关心它是否在原始对象中,而不是子类。

可能我可以将我的 JSON 字符串设为字典,但即便如此我也不知道如何将它映射到带有子类的模型。

希望我可以拥有无​​限数量的子类,并且 JSON 字符串可以将平面 JSON 模型映射到模型。

4

1 回答 1

4

您可以使用 a 以JsonConverter通用方式执行此操作,使用 aContractResolver对正在反序列化的类或其包含的类中的属性进行分组和填充(视情况而定)。

你没有要求序列化,只要求反序列化,所以这就是这样做的:

public class JsonFlatteningConverter : JsonConverter
{
    readonly IContractResolver resolver;

    public JsonFlatteningConverter(IContractResolver resolver)
    {
        if (resolver == null)
            throw new ArgumentNullException();
        this.resolver = resolver;
    }

    public override bool CanConvert(Type objectType)
    {
        return resolver.ResolveContract(objectType) is JsonObjectContract;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;

        JObject jObject = JObject.Load(reader);
        var contract = (JsonObjectContract)resolver.ResolveContract(objectType); // Throw an InvalidCastException if this object does not map to a JObject.

        existingValue = existingValue ?? contract.DefaultCreator();

        if (jObject.Count == 0)
            return existingValue;

        var groups = jObject.Properties().GroupBy(p => p.Name.Contains('.') ? p.Name.Split('.').FirstOrDefault() : null).ToArray();
        foreach (var group in groups)
        {
            if (string.IsNullOrEmpty(group.Key))
            {
                var subObj = new JObject(group);
                using (var subReader = subObj.CreateReader())
                    serializer.Populate(subReader, existingValue);
            }
            else
            {
                var jsonProperty = contract.Properties[group.Key];
                if (jsonProperty == null || !jsonProperty.Writable)
                    continue;
                if (jsonProperty != null)
                {
                    var subObj = new JObject(group.Select(p => new JProperty(p.Name.Substring(group.Key.Length + 1), p.Value)));
                    using (var subReader = subObj.CreateReader())
                    {
                        var propertyValue = serializer.Deserialize(subReader, jsonProperty.PropertyType);
                        jsonProperty.ValueProvider.SetValue(existingValue, propertyValue);
                    }
                }
            }
        }
        return existingValue;
    }

    public override bool CanWrite { get { return false; } }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}

然后这样使用它:

        var resolver = new DefaultContractResolver();
        var settings = new JsonSerializerSettings { ContractResolver = resolver, Converters = new JsonConverter[] { new JsonFlatteningConverter(resolver) } };

        var person = JsonConvert.DeserializeObject<Person>(json, settings);

原型小提琴

于 2015-10-09T02:28:43.713 回答