Read and parse JSON in C# / .NET Framework

Raymond Raymond event 2019-11-18 visibility 3,696
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
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts