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. With:
[
["John", "Smith", "23"],
["Paula", "Martin", "54]
]
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;}
}
where index 0 of an inner array maps to First
, index 1 maps to Last
, and index 2 maps to Age
;
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.
Edit:
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…