Monday, November 28, 2016

Unexpected character encountered while parsing value: C. Path '', line 0, position 0. (Deserialize JSON string error)

Hi,
A common issue when deserializing a JSON object 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);
}

0 comments:

Post a Comment