Read Json from file C# code example

Read Json from file C# code example

Read Json

In this article we will be using JObject and JToken from Newtonsoft library to parse the json from a file. You can import Newtonsoft into your object using NuGet package manager available under Tools menu in Visual Studio 2015 or above. I will be putting two methods by which you can read the file as shown below:

Import

using Newtonsoft.Json.Linq;

Code example

JObject oJson = JObject.Parse(File.ReadAllText(@"d:\my.json"));

// read JSON directly from a file
using (StreamReader file = File.OpenText(@"d:\my.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
	JObject oJason_two = (JObject)JToken.ReadFrom(reader);
}

//now you can use any of the object both will have same json

Please leave your comments or queries under comment section also please do subscribe to our blogs to keep your self upto date.

Leave a Reply

Your email address will not be published. Required fields are marked *