SZT
SZT

Reputation: 1966

Deserializing Json can't convert string to object using NewtonSoft

{
  "access_token":"asfdasfdsf",
  "token_type":"bearer",
  "expires_in":15179999,
  "refresh_token":"sdsfsf",
  ".issued":"Sat, 28 Apr 2018 03:05:12 GMT",
  ".expires":"Sat, 20 Oct 2018 19:45:12 GMT",
  "ip_address":"111.111.11.1",
  "client_id":"asdsdfsf",
  "user":"{\r\n \"Active\": true,\r\n \"DisplayName\": \"Sakib Hasan\",\r\n \"Email\": \"[email protected]\",\r\n \"EmailVarified\": true,\r\n \"Language\": \"en-US\",\r\n \"PhoneNumber\": null,\r\n \"ProfileImageUrl\": null,\r\n \"Roles\": [\r\n \"anonymous\",\r\n \"admin\"\r\n ],\r\n \"TenantId\": \"asfsf\",\r\n \"UserName\": \"[email protected]\",\r\n \"FirstName\": null,\r\n \"UserSignup\": false,\r\n \"ProfileImageId\": null,\r\n \"EverLoggedIn\": true,\r\n \"PersonIdentifier\": null,\r\n \"UserId\": \"sdfsff\"\r\n}",
  "may_access":""
}

I'm trying to deserialize the above string to my C# object. My classes look like the following

internal class TokenResponse
{
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [JsonProperty("token_type")]
    public string TokenType { get; set; }

    [JsonProperty("refresh_token")]
    public string RefreshToken { get; set; }

    [JsonProperty("client_id")]
    public string ClientId { get; set; }

    [JsonProperty("user")]
    public TokenUser User { get; set; }
}

internal class TokenUser
{
    [JsonProperty("DisplayName")]
    public string DisplayName { get; set; }

    [JsonProperty("Email")]
    public string Email { get; set; }

    [JsonProperty("ProfileImageUrl")]
    public string ProfileImageUrl { get; set; }

    [JsonProperty("UserName")]
    public string UserName { get; set; }

    [JsonProperty("FirstName")]
    public string FirstName { get; set; }

    [JsonProperty("UserId")]
    public string UserId { get; set; }
}

now when I try to deserialize using Newtonsoft

tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(jsonTokenResponse);

I get cannot convert string to User error. Am I missing something here?

Upvotes: 1

Views: 6021

Answers (2)

SZT
SZT

Reputation: 1966

Answering for future reference.

The problem was with the Json itself. It was treating the 'user' as a string and wasn't converting to User object. So I had to clean it up so DeSerializer(ds) knows it's an object.

json2csharp helped me identify the problem. If you put the above Json in it, it creates a class with string property.

After cleaning it up jsonformatter is a good one to verify if the cleaned up version is still good json.

Upvotes: 1

Akbar Badhusha
Akbar Badhusha

Reputation: 2627

Try like this,

internal class TokenResponse
{
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }

    [JsonProperty("token_type")]
    public string TokenType { get; set; }

    [JsonProperty("refresh_token")]
    public string RefreshToken { get; set; }

    [JsonProperty("client_id")]
    public string ClientId { get; set; }

    [JsonProperty("user")]
    public string User { get; set; }

    [JsonIgnore]
    public TokenUser UserToken { get; set; }

}



internal class TokenUser
{
    [JsonProperty("DisplayName")]
    public string DisplayName { get; set; }

    [JsonProperty("Email")]
    public string Email { get; set; }

    [JsonProperty("ProfileImageUrl")]
    public string ProfileImageUrl { get; set; }

    [JsonProperty("UserName")]
    public string UserName { get; set; }

    [JsonProperty("FirstName")]
    public string FirstName { get; set; }

    [JsonProperty("UserId")]
    public string UserId { get; set; }
}


tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(jsonTokenResponse);
tokenResponse.UserToken = JsonConvert.DeserializeObject<TokenUser>(tokenResponse);

Because your user property is getting a string when it is expecting TokenUser

Upvotes: 2

Related Questions