แปลง JSON เป็นคลาส C# สำหรับ System.Text.Json ชื่อพร็อพเพอร์ตีถูกแปลงเป็น PascalCase และทุกพร็อพเพอร์ตีมี attribute [JsonPropertyName] เพื่อให้ bind กับคีย์เดิมได้ตรงตัว
ช่วยลดงานคัดลอกและพิมพ์เองสำหรับโมเดล request และ response ของ ASP.NET Core ไทป์สำหรับเรียก API ภายนอกด้วย HttpClient.GetFromJsonAsync<T>() และคลาสข้อมูลในแอป Unity หรือ MAUI
ชื่อไทป์ (เช่น PaymentResult).cs แล้วเพิ่ม namespace ของคุณJsonSerializer.Deserialize<PaymentResult>(json) หรือ GetFromJsonAsync<PaymentResult>(url){
"paymentId": "pay_8f2k",
"amount": 15000,
"currency": "KRW",
"approved": true,
"approvedAt": "2024-05-01T12:30:00Z",
"card": { "issuer": "VISA", "last4": "4242" },
"metadata": null
}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?)= string.Empty; หรือ = new(); เพื่อเลี่ยง nullable reference warning CS8618Value ต่อท้าย (เช่น RootValue)// Root: List<RootItem> และคลาสของสมาชิกแปลง response ตัวอย่างจาก API ชำระเงินหรือติดตามพัสดุ แล้วใช้เป็นไทป์ใน await http.GetFromJsonAsync<PaymentResult>(url) แทนการดึงฟิลด์ทีละตัวออกจาก JsonDocument
สร้างคลาสจาก payload ที่บริการภายนอก POST มาหาคุณ แล้วรับเป็นพารามิเตอร์ [FromBody] ทำให้ model binding และ validation ทำงานได้ตามปกติ
การผูกโครงสร้างของไฟล์ข้อมูล JSON ที่แอป Unity หรือ console อ่าน ไว้กับคลาส ทำให้เมื่อไฟล์ข้อมูลเปลี่ยนจะเห็นเป็น deserialization error ทันที
[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; }[JsonPropertyName] แม้คีย์จะตรงกันอยู่แล้ว?System.Text.Json แยกตัวพิมพ์เล็กใหญ่เป็นค่าเริ่มต้น ในโปรเจกต์ console หรือ worker ที่ไม่ได้กำหนด JsonSerializerOptions เอง paymentId จะไม่ bind กับ PaymentId จึงเขียน attribute ไว้เสมอเพื่อให้ทำงานได้ไม่ว่าคอนฟิกจะเป็นอย่างไร
JsonElement? อย่างไร?เช็ก ValueKind ก่อน แล้วใช้ GetString(), GetInt32() หรือ Deserialize<T>() เมื่อรู้ไทป์จริงแล้ว การเปลี่ยน JsonElement เป็นไทป์นั้นจะสะอาดกว่า
ผลลัพธ์ทำตามธรรมเนียม PascalCase ของ C# คีย์เดิมอยู่ใน [JsonPropertyName] จึงเปลี่ยนชื่อพร็อพเพอร์ตีได้อิสระตราบใดที่ attribute ยังอยู่
JsonUtility รองรับเฉพาะฟิลด์ public ของคลาสที่เป็น [Serializable] ไม่รองรับพร็อพเพอร์ตี และไม่สนใจ [JsonPropertyName] ใน Unity ให้ใช้ผลลัพธ์คู่กับแพ็กเกจ Newtonsoft.Json หรือ System.Text.Json แทน