Read and parse JSON in C# / .NET Framework

Raymond Raymond visibility 3,388 event 2019-11-18 access_time 4 years ago language English
more_vert

JSON is commonly used in modern applications for data storage and transfers. Pretty much all programming languages provide APIs to parse JSON. 

There are many libraries you can use in .NET/C# to parse JSON content.

Json.NET (Newtonsoft.Json) is one of the commonly used library. The package information is available on the following page:

https://www.nuget.org/packages/Newtonsoft.Json

Code snippet

using Newtonsoft.Json;
using System;

namespace ConsoleApp1
{
    class Program
    {
        public class MyType
        {
            public string Name { get; set; }
            public int Value { get; set; }
        }
        static void Main(string[] args)
        {
            var jsonStr = "{\"Name\":\"A\",\"Value\":2}";
            Console.WriteLine(jsonStr);
            var jsonObj = JsonConvert.DeserializeObject(jsonStr);
            Console.WriteLine(jsonObj.Name);
            Console.WriteLine(jsonObj.Value);
            Console.ReadLine();
        }
    }
}
More from Kontext
info Last modified by Raymond 4 years ago copyright This page is subject to Site terms.
Like this article?
Share on
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts