Donate

Unexpected character encountered while parsing value: C. Path '', line 0, position 0. (Deserialize JSON String Error In C#)

Hi,
A common issue when deserializing a JSON object in C# is to pass the filepath of the JSON file to the Deserialize() method instead of the string content of that file such as the code below.
var obj = JsonConvert.DeserializeObject<Employee>(@"D:\Data\Employee.json")
When calling the Deserialize() method, you need to make sure that the parameter passed is a JSON string value instead of the file path. Use StreamReader class or File.ReadAllText() to get the content of the JSON file.
using (StreamReader reader = new StreamReader(@"D:\Data\Employee.json")) 
{
 string json = reader.ReadToEnd();
 var obj = JsonConvert.DeserializeObject<Employee>(json);
}

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input