--- lang: ja-jp breaks: true --- # .NET Core で使用する Jsonデシリアライザ「System.Text.Json」 C# 2021-04-22 .NET Framework時代では、「Json.NET」(Newtonsoft.Json.dll)がよく利用されていたが、.Net Core時代では、デフォルトで利用できる「System.Text.Json」が一般的になりそう。 ```csharp= var options = new JsonSerializerOptions(); options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); options.WriteIndented = true; // クラスを指定してデシリアライズ var orders = JsonSerializer.Deserialize<orders_class>(strJson, options); // Dictionaryを指定して、デシリアライズ。 var json_require = JsonSerializer.Deserialize<Dictionary<string, JsonDocument>>(strRequireJson, options); // 見やすく整形された状態でログ出力。 if (dicRequire != null) { log.Info("dicRequire:" + JsonSerializer.Serialize(dicRequire, options)); } // Dictionaryから値を取得。 string tokenId = ""; JsonDocument docJson; if (dicRequire.TryGetValue("tokenId", out docJson)) { if (docJson != null) { if (docJson.RootElement.ValueKind == JsonValueKind.String) { tokenId = docJson.RootElement.GetString(); } } } ``` ###### tags: `System.Text.Json` `.NET Core` `C#` `Json`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up