1
{
  "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\": \"test@testc.om\",\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\": \"test@testc.com\",\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?

Andrew
  • 26,706
  • 9
  • 85
  • 101
SZT
  • 1,771
  • 4
  • 26
  • 53

2 Answers2

2

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

Akbar Badhusha
  • 2,415
  • 18
  • 30
  • 1
    I dont want to ignore it. I want the user data – SZT Apr 28 '18 at 05:24
  • 1
    @Akbar Badhusha told you where the error was. At least he orients you to solve it and he showed you a possible solution (not the only one). Maybe he deserves something... – Alpha75 Apr 28 '18 at 15:16
1

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.

SZT
  • 1,771
  • 4
  • 26
  • 53
  • IF you need to actually deserialize the nested embedded json see [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/q/39154043/3744182). – dbc Apr 28 '18 at 16:26