JSON ใน C#

System.Text.Json คือไลบรารี JSON ที่ทันสมัยใน C#

ประเด็นสำคัญ

System.Text.Json คือไลบรารี JSON ที่ทันสมัยใน C#

แยกวิเคราะห์ JSON

using System.Text.Json;
User user = JsonSerializer.Deserialize<User>(text);

แปลงเป็นสตริง

string text = JsonSerializer.Serialize(user, new JsonSerializerOptions { WriteIndented = true });

ตรวจสอบ JSON

try {
  using JsonDocument doc = JsonDocument.Parse(text);
} catch (JsonException ex) {
  Console.WriteLine(ex.Message);
}