You need to create a structure like this:

public class Friends
{

    public List<FacebookFriend> data {get; set;}
}

public class FacebookFriend
{

    public string id {get; set;}
    public string name {get; set;}
}

Then you should be able to do:

Friends facebookFriends = new JavaScriptSerializer().Deserialize<Friends>(result);

The names of my classes are just an example. You should use proper names.

Adding a sample test:

string json =
    @"{""data"":[{""id"":""518523721"",""name"":""ftyft""}, {""id"":""527032438"",""name"":""ftyftyf""}, {""id"":""527572047"",""name"":""ftgft""}, {""id"":""531141884"",""name"":""ftftft""}]}";

Friends facebookFriends = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Friends>(json);

foreach(var item in facebookFriends.data)
{
    Console.WriteLine("id: {0}, name: {1}", item.id, item.name);
}

Produces:

id: 518523721, name: ftyft
id: 527032438, name: ftyftyf
id: 527572047, name: ftgft
id: 531141884, name: ftftft
Answer from Icarus on Stack Overflow
๐ŸŒ
Newtonsoft
newtonsoft.com โ€บ json โ€บ help โ€บ html โ€บ serializingjson.htm
Serializing and Deserializing JSON
For simple scenarios where you want to convert to and from a JSON string, the SerializeObject and DeserializeObject methods on JsonConvert provide an easy-to-use wrapper over JsonSerializer.
Top answer
1 of 10
298

You need to create a structure like this:

public class Friends
{

    public List<FacebookFriend> data {get; set;}
}

public class FacebookFriend
{

    public string id {get; set;}
    public string name {get; set;}
}

Then you should be able to do:

Friends facebookFriends = new JavaScriptSerializer().Deserialize<Friends>(result);

The names of my classes are just an example. You should use proper names.

Adding a sample test:

string json =
    @"{""data"":[{""id"":""518523721"",""name"":""ftyft""}, {""id"":""527032438"",""name"":""ftyftyf""}, {""id"":""527572047"",""name"":""ftgft""}, {""id"":""531141884"",""name"":""ftftft""}]}";

Friends facebookFriends = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Friends>(json);

foreach(var item in facebookFriends.data)
{
    Console.WriteLine("id: {0}, name: {1}", item.id, item.name);
}

Produces:

id: 518523721, name: ftyft
id: 527032438, name: ftyftyf
id: 527572047, name: ftgft
id: 531141884, name: ftftft
2 of 10
59

Sometimes I prefer dynamic objects:

public JsonResult GetJson()
{
  string res;
  WebClient client = new WebClient();

  // Download string
  string value = client.DownloadString("https://api.instagram.com/v1/users/000000000/media/recent/?client_id=clientId");

  // Write values
  res = value;
  dynamic dyn = JsonConvert.DeserializeObject(res);
  var lstInstagramObjects = new List<InstagramModel>();

  foreach(var obj in dyn.data)
  {
    lstInstagramObjects.Add(new InstagramModel()
    {
      Link = (obj.link != null) ? obj.link.ToString() : "",
      VideoUrl = (obj.videos != null) ? obj.videos.standard_resolution.url.ToString() : "",
      CommentsCount = int.Parse(obj.comments.count.ToString()),
      LikesCount = int.Parse(obj.likes.count.ToString()),
      CreatedTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds((double.Parse(obj.created_time.ToString()))),
      ImageUrl = (obj.images != null) ? obj.images.standard_resolution.url.ToString() : "",
      User = new InstagramModel.UserAccount()
             {
               username = obj.user.username,
               website = obj.user.website,
               profile_picture = obj.user.profile_picture,
               full_name = obj.user.full_name,
               bio = obj.user.bio,
               id = obj.user.id
             }
    });
  }

  return Json(lstInstagramObjects, JsonRequestBehavior.AllowGet);
}
๐ŸŒ
Quora
quora.com โ€บ How-do-you-deserialize-JSON-in-the-C-language-on-Windows
How to deserialize JSON in the C language on Windows - Quora
Answer: You can use the cJSON library or any of a large number of alternatives listed in the C section of JSON.org. cJSON is written to the c89 standard so it will happily compile in GCC, Visual Studio, MinGW and probably every common C compiler.
๐ŸŒ
Newtonsoft
newtonsoft.com โ€บ json โ€บ help โ€บ html โ€บ DeserializeWithJsonSerializerFromFile.htm
Deserialize JSON from a file
// read file into a string and deserialize JSON to a type Movie movie1 = JsonConvert.DeserializeObject<Movie>(File.ReadAllText(@"c:\movie.json")); // deserialize JSON directly from a file using (StreamReader file = File.OpenText(@"c:\movie.json")) { JsonSerializer serializer = new JsonSerializer(); Movie movie2 = (Movie)serializer.Deserialize(file, typeof(Movie)); }
๐ŸŒ
Zltl
zltl.github.io โ€บ json-gen-c
json-gen-c: json-gen-c
โ€Fast, tiny, and friendly code generator that turns your C structs into fully featured JSON serializers/deserializers.
๐ŸŒ
Reddit
reddit.com โ€บ r/csharp โ€บ is there a easy way to deserialize json to a struct?
r/csharp on Reddit: Is there a easy way to deserialize JSON to a struct?
May 9, 2024 -

I'm working on a side project right now where I have a class containing two structs and a string as attributes. I need to read values from a JSON file on the computer, then fill the structs and the string with the values in the JSON.

I deserialized JSON before but was always using classes with primitive-type attributes. It's the first time I need to deserialize JSON to fill a struct. I tried to do the same as with classes, suing JsonProperty annotations and calling JsonConvert.DeserializeObject<T>(json) in my code, but without any success.

Any suggestion online seems to advise creating a custom JsonConverter and it doesn't look really easy. I found one solution using a Dictionary, another using classes, but doesn't seem to be anything using structs. Is that really impossible in C#?

Thanks for any input, it's very appreciated!

EDIT: Here's the JSON that is loaded into the application:

{ "time_prices": { "hour": 19.99, "day": 49.99, "week": 249.99, "month": 999.99 }, "distance_prices": { "free_km": 500, "price_per_km": 0.18 }, "fuel_consumption": 37}

Here's the class representing the JSON; https://dotnetfiddle.net/JOwQtT

The goal is to load that JSON into the class so the values could be accessed to make some price calculations. The values will never be modified. I think they could probably be marked as readonly since once initialized, they will never change.

๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ json-serialization-and-deserialization-in-c-sharp
JSON Serialization and Deserialization in C#
November 19, 2023 - In this article, you will learn about JSON serialization and deserialization in C#. We can implement JSON Serialization/Deserialization by using JavaScriptSerializer class, DataContractJsonSerializer class, JSON.NET library.
๐ŸŒ
Newtonsoft
newtonsoft.com โ€บ json โ€บ help โ€บ html โ€บ deserializeobject.htm
Deserialize an Object
string json = @"{ 'Email': 'james@example.com', 'Active': true, 'CreatedDate': '2013-01-20T00:00:00Z', 'Roles': [ 'User', 'Admin' ] }"; Account account = JsonConvert.DeserializeObject<Account>(json); Console.WriteLine(account.Email); // james@example.com
Find elsewhere
๐ŸŒ
Code Maze
code-maze.com โ€บ home โ€บ how to deserialize json into dynamic object in c#
How to Deserialize JSON Into Dynamic Object in C# - Code Maze
April 4, 2024 - Describe how to deserialize JSON into dynamic object in C# with detail explanation and examples using native and Newtonsoft library
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ standard โ€บ serialization โ€บ system-text-json โ€บ deserialization
How to deserialize JSON in C# - .NET | Microsoft Learn
A common way to deserialize JSON is to have (or create) a .NET class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer.Deserialize method.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ json-deserialize-online
JSON Deserialize Online to parse JSON to hierarchy form.
JSON Deserialize Online is easy to use tool to Deserialize JSON data, observe JSON data in tree mode. Copy, Paste, and Deserialize.
๐ŸŒ
MojoAuth
mojoauth.com โ€บ serialize-and-deserialize โ€บ serialize-and-deserialize-json-with-c
Serialize and Deserialize JSON with C | Serialize & Deserialize Data Across Languages
This guide shows you how to efficiently serialize C data structures into JSON strings and deserialize JSON back into C objects. You'll learn practical techniques to integrate JSON processing seamlessly into your C applications, saving you development time and reducing bugs.
๐ŸŒ
SSOJet
ssojet.com โ€บ serialize-and-deserialize โ€บ serialize-and-deserialize-json-in-c
Serialize and Deserialize JSON in C | Serialize and Deserialize Data in Programming Languages
Exchanging data between systems often involves parsing JSON structures, a task that can become complex and error-prone in C. This guide dives into efficient C libraries and techniques for serializing C data structures into JSON strings and deserializing incoming JSON back into C types.
๐ŸŒ
No Dogma Blog
nodogmablog.bryanhogan.net โ€บ 2023 โ€บ 02 โ€บ simple-deserialization-of-json-from-a-file-in-c
Simple Deserialization of JSON from a File in C# | no dogma blog
February 24, 2023 - This is one of those posts that I write for my future self. Here's how to deserialize JSON from a file in C#. It also lists multiple ways to create the C# model from the JSON.
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ how-do-i-serialize-and-deserialize-json-data-in-c-sharp
How do I serialize and deserialize JSON data in C#?
November 20, 2023 - This code defines classes for Person, Address, and PhoneNumber that match the structure of the JSON data. It then uses the JsonSerializer.Deserialize method to parse the JSON string and convert it into a Person object.
๐ŸŒ
DEV Community
dev.to โ€บ iamcymentho โ€บ serialization-and-deserialization-in-c-a-comprehensive-guide-5bj9
Serialization and Deserialization in C#: A Comprehensive Guide - DEV Community
December 6, 2023 - The JsonSerializer.Serializemethod is then used to convert this object into a JSON-formatted string. Deserialization is the reverse process, converting a serialized string back into an object.
๐ŸŒ
Adobe
helpx.adobe.com โ€บ coldfusion โ€บ cfml-reference โ€บ coldfusion-functions โ€บ functions-c-d โ€บ DeserializeJSON.html
DeserializeJSON Function in ColdFusion
December 4, 2025 - It is useful in ColdFusion applications ... as JavaScript function calls with JSON parameters. The DeserializeJSON function converts each JSON data type directly into the equivalent ColdFusion data type, as follows:...