Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value:

I have a JSON file like below

{
    "Valid": true
}

And the following model

public class Account
{
    public bool Valid { get; set; }
    public Account()
    {
        Valid = true;
    }
}

When running the following code to deserialize

public static void JsonDeserializeTest(Type datatype, string filePath)
{
    Account account = JsonConvert.DeserializeObject<Account>(JsonConvert.DeserializeObject<string>(File.ReadAllText(filePath)));
}

I'm receiving the following error:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: {. Path '', line 1, position 1.'

like image 964
Smirti Avatar asked Oct 18 '25 16:10

Smirti


1 Answers

You have to write your code just like this:

public static void JsonDeserializeTest(Type datatype, string filePath)

 {

    Account account = JsonConvert.DeserializeObject<Account> 
                                 (File.ReadAllText(filePath));
}
like image 74
Ramin Quliyev Avatar answered Oct 20 '25 04:10

Ramin Quliyev