解析HTTP JSON响应 - C#

Mar*_*ark 2 c# json http oauth-2.0

我正在为winforms应用程序进行OAUTH2登录.

我必须使用一些凭据向我们发出请求,服务器将使用令牌和json格式进行响应.

解析令牌值的最佳方法是什么?

这是响应格式:

{
    "access_token":"asdfasdfasdfafbasegfnadfgasdfasdfasdf",
    "expires_in":3600,
    "token_type":"Bearer"
}
Run Code Online (Sandbox Code Playgroud)

Mot*_*oSV 7

使用这些属性创建一个类并使用JSON.NET JsonConvert.SerializeObject方法.

public class MyResponse
{
    [JsonProperty("access_token")]
    public string AccessToken { get; set; }
    [JsonProperty("expires_in")]
    public int ExpiresIn { get; set; }
    [JsonProperty("token_type")]
    public string TokenType { get; set; }
}

MyResponse response = new MyResponse();
// Fill in properties
string json = JsonConvert.SerializeObject(response);
Run Code Online (Sandbox Code Playgroud)