How do I deserialize an array of JSON objects to a C# anonymous type?
The solution is:
string json = @"[{'Name':'Mike'}, {'Name':'Ben'}, {'Name':'Razvigor'}]";
var definition = new[] { new { Name = "" } };
var result = JsonConvert.DeserializeAnonymousType(json, definition);
Of course, since result
is an array, you'll access individual records like so:
string firstResult = result[0].Name;
You can also call .ToList()
and similar methods on it.
You can deserialize to dynamic object by this.
dynamic result = JsonConvert.DeserializeObject(jsonArray);