JSON反序列化-使用JSON.NET将数组索引映射到属性 [英] JSON deserialization - Map array indices to properties with JSON.NET

查看:97
本文介绍了JSON反序列化-使用JSON.NET将数组索引映射到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将二维数组反序列化为.net对象的集合.原因是,数组语法将使我的用户更容易在输入文件中使用.所以我只想将数组的索引映射到目标类型的特定属性.

I want to deserialize a 2-dimensional array to a collection of .net objects. The reason is, array syntax will be easier for my user to work with in an input file. So I just want to map the indices of the arrays to specific properties of my target type.

E.G.使用:

[
     ["John", "Smith", "23"],
     ["Paula", "Martin", "54]
]

我将获得一个Person的两个实例:

I would get two instances of a Person:

public class Person {
    public string First {get;set;}
    public string Last {get;set;}
    public string Age {get;set;}
}

其中内部数组的索引0映射到First,索引1映射到Last,索引2映射到Age;

where index 0 of an inner array maps to First, index 1 maps to Last, and index 2 maps to Age;

是否有扩展Json.NET的方法,以便我可以在反序列化期间进行映射,从而隐藏实现细节?我一直在玩自定义JsonConverter,但是我没有找到太多有关如何使用它的信息.

Is there a way to extend Json.NET so that I can do the mapping during deserialization so the implementation details are hidden? I have been playing around with a custom JsonConverter but I haven't found much info on how to use it.

具体来说,我不确定JsonConverter是否适合使用,并且在弄清楚如何实现CanConvert以及如何使用传递给ReadJson方法的参数方面遇到困难.

Specifically, I'm not sure if JsonConverter is the right thing to use, and I'm having trouble figuring out how to implement CanConvert and how to use the parameters passed to the ReadJson method.

推荐答案

您可以使用 JsonConverter .一个简单的用于此目的的转换器将是:

You can do this with a JsonConverter. A simple converter for this purpose would be:

public class PersonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Person);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;
        var array = JArray.Load(reader);
        var person = (existingValue as Person ?? new Person());
        person.First = (string)array.ElementAtOrDefault(0);
        person.Last = (string)array.ElementAtOrDefault(1);
        person.Age = (string)array.ElementAtOrDefault(2);
        return person;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var person = (Person)value;
        serializer.Serialize(writer, new[] { person.First, person.Last, person.Age });
    }
}

如果特定属性具有非基本类型,则可以使用 将它们反序列化为所需的类型:

If the specific properties have non-primitive types, you can use JToken.ToObject<T>(JsonSerializer) to deserialize them to the required type:

person.First = array.ElementAtOrDefault(0)?.ToObject<string>(serializer);

然后您可以将其应用于您的课程:

Then you can apply it to your class:

[JsonConverter(typeof(PersonConverter))]
public class Person
{
    public string First { get; set; }
    public string Last { get; set; }
    public string Age { get; set; }
}

或在设置中使用它:

var settings = new JsonSerializerSettings { Converters = new [] { new PersonConverter() } };
var list = JsonConvert.DeserializeObject<List<Person>>(json, settings);

这篇关于JSON反序列化-使用JSON.NET将数组索引映射到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆