-
Create a new .NET console project in Visual Studio or Rider, or at the command prompt
dotnet new console
(If you're using VS then either .NET Framework or .NET Core or .NET 5+ is fine)
-
(Optional but a good habit!) Make a git repository
dotnet new gitignore git init git add . git commit -a -m "Initial commit"
-
Add Newtonsoft.Json, the de-facto .NET JSON library.
- In Visual Studio
- right-click the project name and "Manage NuGet packages"
(or Tools, NuGet Package Manager, Manage NuGet Packages for Solution) - Browse, Newtonsoft.Json; in the right-hand panel, Install
- right-click the project name and "Manage NuGet packages"
- Using the dotnet command line tool
dotnet add package Newtonsoft.Json
- In Visual Studio
-
Generate typed classes for our example JSON file
- Use either the online tool at https://json2csharp.com or the offline version from GitHub.
- The two are equivalent: they use the same code generation engine. For this data it's OK to use the website but for real work we should avoid posting real project data to strange websites!
- You might want to tick "Add JsonProperty Attributes" if you're using the website to get normal PascalCase C# property names. This is on by default in the offline version.
- For now let's just put the code it generated into Program.cs, in the namespace but below the Program class:
You'll need to add two
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); public class Menu { [JsonProperty("dishName")] public string DishName { get; set; } [JsonProperty("cost")] public string Cost { get; set; } } public class Root { [JsonProperty("menu")] public List<Menu> Menu { get; set; } }
using
statements too to the top of the file (or use the suggested VS or Rider actions to do this automatically):Note that it has given you the Newtonsoft.Json command you'll need to deserialize a string into these structures in a comment.using Newtonsoft.Json; using System.Collections.Generic;
- Let's rename the top level class from Root to MenuRoot to avoid a clash if we add more JSON to this program:
// MenuRoot myDeserializedClass = JsonConvert.DeserializeObject<MenuRoot>(myJsonResponse); public class Menu { [JsonProperty("dishName")] public string DishName { get; set; } [JsonProperty("cost")] public string Cost { get; set; } } public class MenuRoot { [JsonProperty("menu")] public List<Menu> Menu { get; set; } }
- and let's add code to read in the JSON file and deserialize it into our structure in the Main() method, using the example it gave us plus
File.ReadAllText()
to read the whole file into a string:Again you'll need anothervar menuRoot = JsonConvert.DeserializeObject<MenuRoot>( File.ReadAllText("genericSample.json"));
using
at the top:Note: if you're using Visual Studio or Rider then it will default to starting the program in the directory it has compiled it into, which will be two or three directories below your project directory. There are several ways of solving this but for now it's probably simplest to give the full path to the file here, e.g.using System.IO;
If you're using thevar menuRoot = JsonConvert.DeserializeObject<MenuRoot>( File.ReadAllText(@"c:\work\speedcoding2021\jsonexample\genericSample.json"));
dotnet
command at the console then it will read from the current directory so you won't have to do this.
- Use either the online tool at https://json2csharp.com or the offline version from GitHub.
-
Finally let's do something with the data we've read:
Console.WriteLine("Today's menu"); Console.WriteLine("------------"); foreach (var item in menuRoot.Menu) { Console.WriteLine($" {item.DishName,-30} £ {item.Cost}"); }
and run the project from the IDE, or
dotnet run
from the console.
-
Notifications
You must be signed in to change notification settings - Fork 0
RupW/JsonExampleCSharp
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
Example reading a JSON file in C# using Json2CSharp to generate data classes
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published