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\": \"[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?

2
  • Are you sure this JSON isn't malformed? Commented Apr 28, 2018 at 4:48
  • @TanveerBadar it is a valid json, you can check online Commented Apr 28, 2018 at 5:25

2 Answers 2

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

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

2 Comments

I dont want to ignore it. I want the user data
@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...
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.

1 Comment

IF you need to actually deserialize the nested embedded json see How do I convert an escaped JSON string within a JSON object?.

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.