2

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.

8
  • 3
    Please don't post text as screenshots; post it as text. That way people who are trying to help you can cut-n-paste it into their own editors. Commented Oct 27, 2020 at 16:40
  • 1
    Can you write a small, COMPLETE, runnable program that reproduces the problem? Commented Oct 27, 2020 at 16:41
  • 1
    Verify via logging or the debugger. I expect that this is not the case. Commented Oct 27, 2020 at 16:46
  • 1
    Sorry, I was not clear. Can you write a small reproducer and then paste it into your question so that anyone can run it? If you do that then either you will find the problem yourself, or you will create a program that someone else can look at in the debugger. We can't look at your small reproducer that is on your machine only. Commented Oct 27, 2020 at 16:48
  • 3
    To add context to what @EricLippert is saying there - look at the code in my answer: it is entirely self-contained and runnable. The only ambiguity that is left is that in my code I hard-coded a JSON string to what you say it is, rather than using response.Content.ReadAsStringAsync().Result.ToString() - this immediately suggests that response.Content.ReadAsStringAsync().Result.ToString() does not, in fact, evaluate to the JSON that you are thinking of. In many ways, producing a self-contained example usually tells you (like here) where the problem is. Commented Oct 27, 2020 at 16:50

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

but, i'm a bit lost... when i write : Console.writeline(response.Content.ReadAsStringAsync().Result.ToString()) it's give me the exact same json, so how can the json be what i dont think it is ?
@HoldMy well, that's the bit we can't repro without your endpoint, but what I would do here is change it to var json = await response.Content.ReadAsStringAsync();, and then put in a breakpoint after that line, and test (in the debugger "immediate" window) json == @"{""access_token"":....... etc (the verbatim string literal that I had in my answer)
@HoldMy also, and I cannot emphasize this enough: any time you are doing .Result on a task/awaitable/etc: your code is incorrect (I'm going to add a slight caveat here for advanced uses where you have already tested whether the awaitable completed successfully, in niche cases where you're trying to avoid a state machine; none of that applies to your code, so: the use of .Result is an error)
1

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?

1 Comment

note that usually when you access .Result in this way, it would block at the caller until it is available, so I wouldn't expect it to be empty - although I will acknowledge that .Result is just a bad thing to do anyway, and as such frankly and bizarre outcome is potentially possible

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.