Justin Homes
Justin Homes

Reputation: 3799

Convert string[] of json object to List<T>

I am using json.net for json object parsing. I have a string array that has of Json objects. so instead of 1 string with Json Array, I have each json object on different index of string array.

I need to de-serialize this string[] of json object into List<T>.

If it was string of Json Array to List<T> I would call JsonConvert.DeserializeObject<List<T>>(result);

But for this what would be the best approach to convert string[] of jsonobject into List<T>.

Upvotes: 0

Views: 3145

Answers (2)

PMerlet
PMerlet

Reputation: 2594

You can do that in a very simple way using Linq :

var list = jsonobjects.Select(JsonConvert.DeserializeObject<T>).ToList();

Upvotes: 4

shf301
shf301

Reputation: 31394

You'll probably just have to call DeserializeObject on each string in the array.

var list = new List<T>();
foreach (var jsonString in result)
    list.Add(JsonConvert.DeserializeObject<T>(jsonString);

Upvotes: 2

Related Questions