How do I deserialize an array of JSON objects to a C# anonymous type?

marto

I have no problem deserializing a single json object

string json = @"{'Name':'Mike'}";

to a C# anonymous type:

var definition = new { Name = ""};
var result = JsonConvert.DeserializeAnonymousType(json, definition);

But when I have an array:

string jsonArray = @"[{'Name':'Mike'}, {'Name':'Ben'}, {'Name':'Razvigor'}]";

I am stuck.

How can it be done?

Marc.2377

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In C#.NET, how do I deserialize a JSON object that is not an array of objects, and has a unique name for every object?

How do I deserialize a JSON array without needing a wrapper type?

How do I serialize a C# anonymous type to a JSON string?

How do I get System.Text.Json to deserialize objects into their original type?

How do I deserialize a JSON representation containing array of objects using jackson?

How do I, using XStream, serialize/deserialize objects in a type hierarchy?

How do I deserialize a json with nested array in c# using Newtonsoft.Json

How do I deserialize a JSON array using Newtonsoft.Json

How do I deserialize a nested JSON array using GSON?

How do I deserialize a JSON array that contains only values?

How do I deserialize a JSON array(flat) and ignore some token

How to Deserialize Array of JSON Objects in Java

How to deserialize json array of objects into object with properties?

C# Json.Net - How to deserialize an array of objects containing an array of other objects

How do I deserialize a tree of objects into a DTO?

How do I deserialize JSON correctly in C#

How to deserialize multiple JSON objects in C#?

How do I create an Array of Objects in C?

WPF - Json.NET: How do I deserialize multiple objects from Json and put it in an List?

How do I deserialize this type of data structure

How do I deserialize into trait, not a concrete type?

How do I group multiples of an array of json objects into new objects?

How do I group by using anonymous type

How do I deserialize each element of a json array into their proper subclasses based on one of the element's values? (C#)

How can I deserialize json array?

How do I deserialize JSON arrays and bind resulting objects to WPF DataGrids?

How do I deserialize JSON.NET objects that don't have names?

How do I deserialize JSON with optional properties?

How do I deserialize a Json object?