I have a List of JObject getting from a method which I need to convert into a named/defined .NET Object.
List<JObject> lst=getData();
I need to parse the lst into List of NamedObject
something like
List<MyModel> _lst=parse(lst)
How can I accomplish that?
I know JOBject.toObject will work if a single element. But in my case its JObject array.
CodePudding user response:
To construct your List<MyModel>
you may combine JToken.ToObject<T>()
to deserialize a JObject
with a LINQ Select()
method to project every item in your intermediate lst
to your model as follows:
var modelList = lst.Select(o => o.ToObject<MyModel>()).ToList();