4

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.'

1
  • JsonConvert.DeserializeObject<string>( ? why are you trying to decode json twice? Does the file contain a json string literal like "this is a string"? Commented Aug 10, 2022 at 5:55

1 Answer 1

3

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));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it is worked. also i tried by modifying JSON file like this "{ 'Valid': true }"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.