HoldMy
HoldMy

Reputation: 121

Empty class when i deserialise json string to c# object

Here is the Json string printed with Console.Writeline :

{"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvYXBpLmRydW1zdGlrLmFwcFwvYXBpXC9sb2dpbiIsImlhdCI6MTYwMzgxNTcxMywiZXhwIjoxNjAzODE5MzEzLCJuYmYiOjE2MDM4MTU3MTMsImp0aSI6InJhU1dJSHBJaWR0YnhjTUUiLCJzdWIiOjQ1LCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.aaQoQVKTSMFWCEOMv9psVsMeOJqpC5giLfwZ0Uic444","token_type":"bearer","expires_in":3600}

enter image description here

I want to build a c# object :

public class eltoken
    {
        [JsonProperty("access_token")]
        public string AccesToken { get; set; }
        [JsonProperty("token_type")]
        public string TokenType { get; set; }
        [JsonProperty("expires_in")]

        public long ExpiresIn { get; set; }
    }

 eltoken test = JsonConvert.DeserializeObject<eltoken>(response.Content.ReadAsStringAsync().Result.ToString());

                    //------------
                    Console.WriteLine(test.AccesToken);

But I dont uderstand why its empty.

Upvotes: 2

Views: 333

Answers (2)

Sebs29
Sebs29

Reputation: 27

I think the issue is that you are not waiting for the result to be read. Then response.Content.ReadAsStringAsync().Result is empty

I suggest that you have a look at this question and answer: Should I await ReadAsStringAsync() if I awaited the response that I'm performing ReadAsStringAsync() on?

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1063338

You probably haven't awaited correctly. Firstly, this is not the correct way to use an async API:

eltoken test = JsonConvert.DeserializeObject<eltoken>(response.Content.ReadAsStringAsync().Result.ToString());

Try instead:

var json = await response.Content.ReadAsStringAsync();
// debug write/inspect json here
eltoken test = JsonConvert.DeserializeObject<eltoken>(json);

Fundamentally, the deserialize step is fine, so the problem is probably that the JSON isn't what you think it is:

using Newtonsoft.Json;
using System;

class P
{
    static void Main()
    {
        var json = @"{""access_token"":""eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvYXBpLmRydW1zdGlrLmFwcFwvYXBpXC9sb2dpbiIsImlhdCI6MTYwMzgxNTcxMywiZXhwIjoxNjAzODE5MzEzLCJuYmYiOjE2MDM4MTU3MTMsImp0aSI6InJhU1dJSHBJaWR0YnhjTUUiLCJzdWIiOjQ1LCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.aaQoQVKTSMFWCEOMv9psVsMeOJqpC5giLfwZ0Uic444"",""token_type"":""bearer"",""expires_in"":3600}";
        eltoken test = JsonConvert.DeserializeObject<eltoken>(json);
        Console.WriteLine(test.ExpiresIn);
        Console.WriteLine(test.TokenType);
        Console.WriteLine(test.AccesToken);
    }
}
public class eltoken
{
    [JsonProperty("access_token")]
    public string AccesToken { get; set; }
    [JsonProperty("token_type")]
    public string TokenType { get; set; }
    [JsonProperty("expires_in")]
    public long ExpiresIn { get; set; }
}

which outputs, as expected:

3600
bearer
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvYXBpLmRydW1zdGlrLmFwcFwvYXBpXC9sb2dpbiIsImlhdCI6MTYwMzgxNTcxMywiZXhwIjoxNjAzODE5MzEzLCJuYmYiOjE2MDM4MTU3MTMsImp0aSI6InJhU1dJSHBJaWR0YnhjTUUiLCJzdWIiOjQ1LCJwcnYiOiI4N2UwYWYxZWY5ZmQxNTgxMmZkZWM5NzE1M2ExNGUwYjA0NzU0NmFhIn0.aaQoQVKTSMFWCEOMv9psVsMeOJqpC5giLfwZ0Uic444

Upvotes: 4

Related Questions