แปลง JSON เป็น class ของ C#!

ป้อน JSON เพื่อสร้างโค้ด class ของ C# รวมถึงออบเจ็กต์ที่ซ้อนกัน! รองรับ System.Text.Json [JsonPropertyName], .NET, ASP.NET Core
JSON
ผลลัพธ์
 

แปลง JSON เป็น class ของ C#!

แปลง JSON เป็นคลาส C# สำหรับ System.Text.Json ชื่อพร็อพเพอร์ตีถูกแปลงเป็น PascalCase และทุกพร็อพเพอร์ตีมี attribute [JsonPropertyName] เพื่อให้ bind กับคีย์เดิมได้ตรงตัว

ช่วยลดงานคัดลอกและพิมพ์เองสำหรับโมเดล request และ response ของ ASP.NET Core ไทป์สำหรับเรียก API ภายนอกด้วย HttpClient.GetFromJsonAsync<T>() และคลาสข้อมูลในแอป Unity หรือ MAUI

วิธีใช้

  1. วาง JSON ที่เป็น response หรือไฟล์คอนฟิก
  2. ใส่ชื่อคลาสรากใน ชื่อไทป์ (เช่น PaymentResult)
  3. นำคลาสที่ได้ไปใส่ในไฟล์ .cs แล้วเพิ่ม namespace ของคุณ
  4. ใช้งานกับ JsonSerializer.Deserialize<PaymentResult>(json) หรือ GetFromJsonAsync<PaymentResult>(url)

ตัวอย่าง

อินพุต (JSON)
{
  "paymentId": "pay_8f2k",
  "amount": 15000,
  "currency": "KRW",
  "approved": true,
  "approvedAt": "2024-05-01T12:30:00Z",
  "card": { "issuer": "VISA", "last4": "4242" },
  "metadata": null
}
เอาต์พุต (C# · System.Text.Json)
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

public class PaymentResult
{
    [JsonPropertyName("paymentId")]
    public string PaymentId { get; set; } = string.Empty;

    [JsonPropertyName("amount")]
    public int Amount { get; set; }

    [JsonPropertyName("currency")]
    public string Currency { get; set; } = string.Empty;

    [JsonPropertyName("approved")]
    public bool Approved { get; set; }

    [JsonPropertyName("approvedAt")]
    public string ApprovedAt { get; set; } = string.Empty;

    [JsonPropertyName("card")]
    public Card Card { get; set; } = new();

    [JsonPropertyName("metadata")]
    public JsonElement? Metadata { get; set; }
}

public class Card
{
    [JsonPropertyName("issuer")]
    public string Issuer { get; set; } = string.Empty;

    [JsonPropertyName("last4")]
    public string Last4 { get; set; } = string.Empty;
}

ทุกพร็อพเพอร์ตีมี [JsonPropertyName] ดังนั้น paymentId ↔ PaymentId จับคู่กันได้ไม่ว่ากฎตัวพิมพ์จะเป็นแบบไหน metadata มีแต่ค่า null จึงไม่รู้ไทป์และกลายเป็น JsonElement? พร็อพเพอร์ตีที่เป็นสตริงและออบเจ็กต์ได้ initializer string.Empty / new() เพื่อให้โค้ดคอมไพล์ได้โดยไม่มี nullable warning

ฟีเจอร์ที่รองรับ

  • [JsonPropertyName("originalKey")] บนทุกพร็อพเพอร์ตี พร้อม using System.Text.Json.Serialization;
  • ไทป์: string, int, long สำหรับจำนวนเต็มที่เกิน 32 บิต, double สำหรับทศนิยม, bool และ List<T> สำหรับอาร์เรย์
  • ค่าที่ไทป์ปนกันหรือไม่รู้ไทป์จะเป็น JsonElement และเพิ่ม using System.Text.Json;
  • พร็อพเพอร์ตีที่ขาดไปในบางรายการหรือมีค่า null จะเป็น nullable (int?, string?)
  • พร็อพเพอร์ตีที่จำเป็นได้ initializer = string.Empty; หรือ = new(); เพื่อเลี่ยง nullable reference warning CS8618
  • C# ไม่อนุญาตให้พร็อพเพอร์ตีมีชื่อเดียวกับคลาสของมัน จึงเติม Value ต่อท้าย (เช่น RootValue)
  • อาร์เรย์ระดับบนสุดจะได้คอมเมนต์ // Root: List<RootItem> และคลาสของสมาชิก

กรณีการใช้งาน

เรียก API ภายนอกด้วย HttpClient

แปลง response ตัวอย่างจาก API ชำระเงินหรือติดตามพัสดุ แล้วใช้เป็นไทป์ใน await http.GetFromJsonAsync<PaymentResult>(url) แทนการดึงฟิลด์ทีละตัวออกจาก JsonDocument

โมเดล payload ของ webhook ใน ASP.NET Core

สร้างคลาสจาก payload ที่บริการภายนอก POST มาหาคุณ แล้วรับเป็นพารามิเตอร์ [FromBody] ทำให้ model binding และ validation ทำงานได้ตามปกติ

appsettings และไฟล์ข้อมูลเกม

การผูกโครงสร้างของไฟล์ข้อมูล JSON ที่แอป Unity หรือ console อ่าน ไว้กับคลาส ทำให้เมื่อไฟล์ข้อมูลเปลี่ยนจะเห็นเป็น deserialization error ทันที

ข้อควรระวังและข้อจำกัด

  • ไม่มีผลลัพธ์สำหรับ Newtonsoft.Json ([JsonProperty]) ถ้าใช้ Json.NET ให้เปลี่ยน attribute เป็น [JsonProperty("key")]
  • สตริงวันที่ยังเป็น string สำหรับค่า ISO 8601 แค่เปลี่ยนไทป์เป็น DateTime หรือ DateTimeOffset System.Text.Json ก็พาร์สให้ได้
  • new() แบบ target-typed และ string? ต้องใช้ C# 9+ ที่เปิด nullable context (ค่าเริ่มต้นตั้งแต่ .NET 6)
  • ไม่สร้าง record หรือ required ถ้าต้องการโมเดลแบบ immutable ให้เปลี่ยน { get; set; } เป็น { get; init; }
  • ทุกคลาสออกมาในบล็อกเดียว การแยกไฟล์และ namespace ต้องทำเอง

คำถามที่พบบ่อย

ทำไมต้องใส่ [JsonPropertyName] แม้คีย์จะตรงกันอยู่แล้ว?

System.Text.Json แยกตัวพิมพ์เล็กใหญ่เป็นค่าเริ่มต้น ในโปรเจกต์ console หรือ worker ที่ไม่ได้กำหนด JsonSerializerOptions เอง paymentId จะไม่ bind กับ PaymentId จึงเขียน attribute ไว้เสมอเพื่อให้ทำงานได้ไม่ว่าคอนฟิกจะเป็นอย่างไร

จะอ่านพร็อพเพอร์ตี JsonElement? อย่างไร?

เช็ก ValueKind ก่อน แล้วใช้ GetString(), GetInt32() หรือ Deserialize<T>() เมื่อรู้ไทป์จริงแล้ว การเปลี่ยน JsonElement เป็นไทป์นั้นจะสะอาดกว่า

เก็บชื่อพร็อพเพอร์ตีเป็น camelCase ได้ไหม?

ผลลัพธ์ทำตามธรรมเนียม PascalCase ของ C# คีย์เดิมอยู่ใน [JsonPropertyName] จึงเปลี่ยนชื่อพร็อพเพอร์ตีได้อิสระตราบใดที่ attribute ยังอยู่

ใช้กับ JsonUtility ของ Unity ได้ไหม?

JsonUtility รองรับเฉพาะฟิลด์ public ของคลาสที่เป็น [Serializable] ไม่รองรับพร็อพเพอร์ตี และไม่สนใจ [JsonPropertyName] ใน Unity ให้ใช้ผลลัพธ์คู่กับแพ็กเกจ Newtonsoft.Json หรือ System.Text.Json แทน