Json to object in C#
To convert a Json file to an object we can useJsonConvert.DeserializeObject()
method by using using Newtonsoft.Json;
namespace, this method takes Json as string so you can also read file using File.ReadAllText()
from using System.IO;
namespace which takes the path as string. Example
In below example we create a Class called Person and we are going to read person.jsonusing System;
using System.IO;
using Newtonsoft.Json;
public class Person {
public string firstName { get; set; }
public string lastName { get; set; }
}
public class Program
{
public static void Main()
{
Person persons = JsonConvert.DeserializeObject<Person>(File.ReadAllText(@"c:\person.json"));
}
}