0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

続 JSON をクラスにバインドする (C#)

Last updated at Posted at 2022-05-10

続 JSON をクラスにバインドする (C#)

# 以下の記事が書かれた時の版数は .NET Core 3.1 (3.1.24) となります.

過去に DataContractJsonSerializer で書いた JSON をクラスにバインドする (C#)System.Text.Json で焼き直し.

JSON を C# のクラスにバインドする. まずバインド先のクラスを作る. 都合により Azure AD のレスポンス.

using System.Text.Json;
using System.Text.Json.Serialization;

namespace ConsoleApp1
{
    public sealed class AzureADResponse
    {
        [JsonPropertyName("token_type")]
        public string TokenType { get; set; }

        [JsonPropertyName("scope")]
        public string Scope { get; set; }

        [JsonPropertyName("expires_in")]
        public int ExpiresIn { get; set; }

        [JsonPropertyName("ext_expires_in")]
        public int ExtExpiresIn { get; set; }

        [JsonPropertyName("access_token")]
        public string AccessToken { get; set; }

        [JsonPropertyName("refresh_token")]
        public string RefreshToken { get; set; }

        [JsonPropertyName("id_token")]
        public string IdToken { get; set; }
    }
}

後は Azure AD のレスポンスの文字列が変数 s に入っているとすれば、以下でバインドできる.

JsonSerializer.Deserialize<AzureADResponse>(s);
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?