You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings.

JSON

The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name.

{"Id": 1, "Name": "biofractal"}

C#

Use JsonConvert.DeserializeObject<dynamic>() to deserialize this string into a dynamic type then simply access its properties in the usual way.

dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.Id;
var name= results.Name;

If you specify the type of the results variable as dynamic, instead of using the var keyword, then the property values will correctly deserialize, e.g. Id to an int and not a JValue (thanks to GFoley83 for the comment below).

Note: The NuGet link for the Newtonsoft assembly is http://nuget.org/packages/newtonsoft.json.

Package: You can also add the package with nuget live installer, with your project opened just do browse package and then just install it install, unistall, update, it will just be added to your project under Dependencies/NuGet

Answer from biofractal on Stack Overflow
🌐
Newtonsoft
newtonsoft.com › json › help › html › Samples.htm
Samples
Over 100 code samples covering Json.NET's most commonly used functionality.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › serialization › system-text-json › migrate-from-newtonsoft
Migrate from Newtonsoft.Json to System.Text.Json - .NET | Microsoft Learn
Newtonsoft.Json accepts non-string values, such as a number or the literals true and false, for deserialization to properties of type string. Here's an example of JSON that Newtonsoft.Json successfully deserializes to the following class:
🌐
FriendlyUsers Tech Blog
friendlyuser.github.io › posts › tech › 2023 › A_Comprehensive_Guide_to_Newtonsoft.Json
A Comprehensive Guide to Newtonsoft.Json - FriendlyUsers Tech Blog
Similar to serialization, JsonConvert provides various options to customize the deserialization process. You can use attributes, custom converters, and specify error handling during deserialization. Here’s an example that demonstrates some of these features: using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; public class Person { [JsonProperty("full_name")] public string Name { get; set; } public int Age { get; set; } [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; } } public enum Gender { Male, Female } public class Program { public static v
🌐
Medium
medium.com › @ieplt › comprehensive-guide-to-using-json-and-newtonsoft-json-in-net-ac67b7963e35
Comprehensive Guide to Using JSON and Newtonsoft.Json in .NET | by İbrahim Emre POLAT | Medium
June 14, 2024 - Newtonsoft.Json makes it simple to deserialize JSON strings into .NET objects. Let’s explore how to deserialize JSON into objects using Newtonsoft.Json. To deserialize JSON into a C# object, you can use the JsonConvert.DeserializeObject<T> method. Here's a basic example:
Top answer
1 of 12
296

You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings.

JSON

The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name.

{"Id": 1, "Name": "biofractal"}

C#

Use JsonConvert.DeserializeObject<dynamic>() to deserialize this string into a dynamic type then simply access its properties in the usual way.

dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.Id;
var name= results.Name;

If you specify the type of the results variable as dynamic, instead of using the var keyword, then the property values will correctly deserialize, e.g. Id to an int and not a JValue (thanks to GFoley83 for the comment below).

Note: The NuGet link for the Newtonsoft assembly is http://nuget.org/packages/newtonsoft.json.

Package: You can also add the package with nuget live installer, with your project opened just do browse package and then just install it install, unistall, update, it will just be added to your project under Dependencies/NuGet

2 of 12
278

If you just need to get a few items from the JSON object, I would use Json.NET's LINQ to JSON JObject class. For example:

JToken token = JObject.Parse(stringFullOfJson);

int page = (int)token.SelectToken("page");
int totalPages = (int)token.SelectToken("total_pages");

I like this approach because you don't need to fully deserialize the JSON object. This comes in handy with APIs that can sometimes surprise you with missing object properties, like Twitter.

Documentation: Serializing and Deserializing JSON with Json.NET and LINQ to JSON with Json.NET

🌐
Newtonsoft
newtonsoft.com › json › help › html › introduction.htm
Introduction
It is used in many major open source projects, including: Mono, an open source implementation of the .NET framework; RavenDB, a JSON based document database; ASP.NET SignalR, an async library for building real-time, multi-user interactive web applications; and ASP.NET Core, Microsoft's web ...
🌐
ZetCode
zetcode.com › csharp › json-net
C# Json.NET - working with JSON with Newtonsoft Json.NET
In this article we work with Newtonsoft Json.NET library. In the standard library, we can alternatively use System.Text.Json. JsonConvert provides methods for converting between .NET types and JSON types. JsonConvert.SerializeObject serializes the specified object to a JSON string. JsonConvert.DeserializeObject deserializes the JSON to a .NET object. In the following example, we serialize an object to a JSON string.
🌐
GitHub
github.com › JamesNK › Newtonsoft.Json
GitHub - JamesNK/Newtonsoft.Json: Json.NET is a popular high-performance JSON framework for .NET · GitHub
Json.NET is a popular high-performance JSON framework for .NET - JamesNK/Newtonsoft.Json
Starred by 11.3K users
Forked by 3.3K users
Languages   C#
Find elsewhere
🌐
C# Corner
c-sharpcorner.com › article › newtonsoft-json-deserialize-c-sharp-example
Newtonsoft JSON Deserialize in C# with Example
August 17, 2023 - Step 6. Create a new class, “module post,” for requesting json parse from the rest api client. Write the following program listing. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json.Linq; using System.Net; using System.IO; namespace newtonsoft_json { public class modulePost { public static string urlServer = "http://localhost/json/ws.php"; public void setURL(string url) { urlServer = url; } public string getURL() { return urlServer; } public string Post(string data) { string DataFromPHP = null; try { string url = getURL(); byte[] bu
🌐
NuGet
nuget.org › packages › newtonsoft.json
NuGet Gallery | Newtonsoft.Json 13.0.4
JArray array = new JArray(); array.Add("Manual text"); array.Add(new DateTime(2000, 5, 23)); JObject o = new JObject(); o["MyArray"] = array; string json = o.ToString(); // { // "MyArray": [ // "Manual text", // "2000-05-23T00:00:00" // ] // ...
🌐
Newtonsoft
newtonsoft.com › json › help › html › CreatingLINQtoJSON.htm
Creating JSON
List<Post> posts = GetPosts(); JObject rss = new JObject( new JProperty("channel", new JObject( new JProperty("title", "James Newton-King"), new JProperty("link", "http://james.newtonking.com"), new JProperty("description", "James Newton-King's blog."), new JProperty("item", new JArray( from p in posts orderby p.Title select new JObject( new JProperty("title", p.Title), new JProperty("description", p.Description), new JProperty("link", p.Link), new JProperty("category", new JArray( from c in p.Categories select new JValue(c))))))))); Console.WriteLine(rss.ToString()); //{ // "channel": { // "t
🌐
Newtonsoft
newtonsoft.com › json › help › html › readingwritingjson.htm
Basic Reading and Writing JSON
They are located in the Newtonsoft.Json.Linq namespace. These objects allow you to use LINQ to JSON objects with objects that read and write JSON, such as the JsonSerializer. For example you can deserialize from a LINQ to JSON object into a regular .NET object and vice versa.
🌐
Newtonsoft
newtonsoft.com › json › help › html › ParseJsonObject.htm
Parsing JSON Object using JObject.Parse
string json = @"{ CPU: 'Intel', Drives: [ 'DVD read/writer', '500 gigabyte hard drive' ] }"; JObject o = JObject.Parse(json); Console.WriteLine(o.ToString()); // { // "CPU": "Intel", // "Drives": [ // "DVD read/writer", // "500 gigabyte hard drive" // ] // }
🌐
Newtonsoft
newtonsoft.com › json
Json.NET - Newtonsoft
Create, parse, query and modify JSON using Json.NET's JObject, JArray and JValue objects.
🌐
Newtonsoft
newtonsoft.com › json › help › html › serializationguide.htm
Serialization Guide
In Json.NET, dynamic properties are serialized and deserialized exactly the same as untyped objects: because dynamic isn't an actual type, Json.NET falls back to deserializing the JSON as LINQ to JSON objects. The second usage of dynamic in .NET are by the types that implement IDynamicMetaObjectProvider. This interface lets the implementer create dynamic objects that intercept the property and method calls on an object and use them. ExpandoObject is a good example of a dynamic object.
🌐
Newtonsoft
newtonsoft.com › json › help › html › QueryJson.htm
Querying JSON
string json = @"{ 'channel': { 'title': 'James Newton-King', 'link': 'http://james.newtonking.com', 'description': 'James Newton-King\'s blog.', 'item': [ { 'title': 'Json.NET 1.3 + New license + Now on CodePlex', 'description': 'Announcing the release of Json.NET 1.3, the MIT license and the source on CodePlex', 'link': 'http://james.newtonking.com/projects/json-net.aspx', 'category': [ 'Json.NET', 'CodePlex' ] }, { 'title': 'LINQ to JSON beta', 'description': 'Announcing LINQ to JSON', 'link': 'http://james.newtonking.com/projects/json-net.aspx', 'category': [ 'Json.NET', 'LINQ' ] } ] } }";
🌐
Particular
docs.particular.net › nservicebus › serialization › newtonsoft
Json.NET Serializer • Newtonsoft Serializer • Particular Docs
2 weeks ago - Newtonsoft JSON Serializer sample NServiceBus sample that shows how to use the Newtonsoft JSON serializer in an endpoint.
🌐
Newtonsoft
newtonsoft.com › json › help › html › t_newtonsoft_json_linq_jobject.htm
JObject Class
Examples · Parsing a JSON Object from Text · Copy · string json = @"{ CPU: 'Intel', Drives: [ 'DVD read/writer', '500 gigabyte hard drive' ] }"; JObject o = JObject.Parse(json); See Also · Newtonsoft.Json.Linq Namespace ·
🌐
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(); ...
🌐
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.