๐ŸŒ
Code Beautify
codebeautify.org โ€บ json-deserialize-online
JSON Deserialize Online to parse JSON to hierarchy form.
This tool allows loading the JSON URL. Use your JSON REST URL to Deserialize. Click on the Load URL button, Enter URL and Submit.
๐ŸŒ
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.
๐ŸŒ
Json2CSharp
json2csharp.com
Convert JSON to C# Classes Online - Json2CSharp Toolkit
When you copy the returned classes in the directory of your solution, you can deserialize your JSON response using the 'Root' class using any deserializer like Newtonsoft.
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ json-parser
JSON Parser Online to parse JSON
This Parse JSON Online tool is very powerful. This will show data in a tree view which supports image viewer on hover. It also validates your data and shows errors in great detail. It's a wonderful tool crafted for JSON lovers who are looking to deserialize JSON online.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
.NET Fiddle
dotnetfiddle.net โ€บ YC7SE1
[Fork] Json Deserialization | C# Online Compiler | .NET Fiddle
[Fork] Json Deserialization | Test your C# code online with .NET Fiddle code editor.
๐ŸŒ
.NET Fiddle
dotnetfiddle.net โ€บ WJwvm3
Json Deserialization | C# Online Compiler | .NET Fiddle
Json Deserialization | Test your C# code online with .NET Fiddle code editor.
๐ŸŒ
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
๐ŸŒ
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.
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);
}
๐ŸŒ
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.
๐ŸŒ
Onlinewebtoolkit
onlinewebtoolkit.com โ€บ unserialize
Serialize and Unserialize Converter Tool | Convert Array, Object, Serialized Data, Json, Xml, Http Query
This online tool convert serialized data, array, object, json, xml, http query to unserialized data (print_r, var_dump, var_export), serialized, json, xml, http query output data
๐ŸŒ
QuickType
quicktype.io
Convert JSON to Swift, C#, TypeScript, Objective-C, Go, Java, C++ and more
quicktype generates types and helper code for reading JSON in C#, Swift, JavaScript, Flow, Python, TypeScript, Go, Rust, Objective-C, Kotlin, C++ and more. Customize online with advanced options, or download a command-line tool.
๐ŸŒ
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)); }
๐ŸŒ
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.