43

I am trying to deserialize a JSON string received from a Web API

try
{
    string r = await App.client.GetUser();

    App.Authentication = JsonConvert.DeserializeObject<ApiResult>(r);

    await DisplayAlert("TEST", App.Authentication.ToString(), "OK");

    Application.Current.MainPage = new Schedule();
}
catch (Exception p)
{
    await DisplayAlert("Getting Authentication failed", p.ToString(), "TEST");
}

However it gives the error: Could not Cast or Convert System.String to App1.ApiResult App.Authentication = JsonConvert.DeserializeObject<ApiResult>(r);

App.Authentication:

public static ApiResult Authentication = new ApiResult();`

JSON string:

"\"{\\"status\\":\\"0\\",\\"message\\":{\\"ID\\":5,\\"FirstName\\":\\"John\\",\\"LastName\\":\\"Doe\\",\\"Email\\":\\"[email protected]\\",\\"Password\\":\\"testPass\\",\\"CreationDate\\":\\"2016-10-26T15:01:08\\",\\"RoleID\\":1,\\"doorCode\\":9999}}\""

ApiResult Class:

public class ApiResult
{
    public string status { get; set; }
    public Account message { get; set; }
}

Account Class:

public class Account
{
    public string status { get; set; }
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public DateTime CreationDate { get; set; }
    public int RoleID { get; set; }
    public int doorCode { get; set; }
}

The full error message:

{"Error converting value \"{\"status\":\"0\",\"message\":{\"ID\":5,\"FirstName\":\"John\",\"LastName\":\"Doe\",\"Email\":\"[email protected]\",\"Password\":\"testPass\",\"CreationDate\":\"2016-10-26T15:01:08\",\"RoleID\":1,\"doorCode\":9999}}\" to type 'App1.ApiResult'. Path '', line 1, position 232."}

2
  • jsonformatter.curiousconcept.com string r = await App.client.GetUser(); you can check "r" value here. Commented Nov 25, 2016 at 12:06
  • Looks like it cant parse date from your message, try to parse it without CreationDate. Commented Nov 25, 2016 at 12:14

3 Answers 3

98

It appears that the json you receive has been serialized twice - first from ApiResult to string, then to string again:

"\"{\\"status\\":\\"0\\",\\"message\\":...

The first double-quote might be added by your debugger, but the second (the escaped \" one) really appears to be part of the data you're processing. The error message also makes sense this way, it deserializes a string and then attempts to cast it to an ApiResult.

Try deserializing the data as a string and then deserializing its result to an ApiResult, to be sure this is the case - and if so, the server code will need to be changed.

Sign up to request clarification or add additional context in comments.

4 Comments

Turns out it was serialized twice. I didn't make the API so I wasn't aware of that. Thank you.
In my case, my ASP.NET Core server was returning Ok(JsonConvert.SerializeObject(foo)). Changing it to Ok(foo) fixed the double-serialization issue.
JsonConvert.DeserializeObject<ApiResult>(JsonConvert.DeserializeObject<string>(apiResponse)). First deserialize into string and then deserialize as ApiResult model class.
The best answers are the simplest ones. Love you! Thank you so much!
27

Below code worked for me along the lines of to C.Evenhuis answer,

   var content = response.Content.ReadAsStringAsync().Result;              
   var jsonResult = JsonConvert.DeserializeObject(content).ToString();
   var result= JsonConvert.DeserializeObject<Model>(jsonResult);

Here Content is similar to - ""{\"Id\":\"92209\",\"operatorId\":100000,\"Status\":true, .....

2 Comments

On jsonResult.. I am getting Additional text encountered after finished reading JSON content error...
This worked for me, thanks. my request was serialized twice, to deseriliaze it, i had to cast it to string using toString() before i called Jsonconvert.Deseriliaze for the second time.
0

Try using App.Authentication = JObject.Parse(request.Content.ReadAsStringAsync().Result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.