@SerialName และการจับคู่ฟิลด์แบบกำหนดเอง
จับคู่ชื่อ Kotlin กับคีย์ JSON ที่ไม่ตรงกัน
@SerialName และการจับคู่ฟิลด์แบบกำหนดเอง เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Names Do Not Always Match
Real APIs love keys like user_name, but Kotlin prefers userName. @SerialName lets the two disagree peacefully.
Rename a Single Field
Put @SerialName above a property to tell the serializer which JSON key it really maps to.
@SerialName("user_name") val userName: StringKotlin Stays Idiomatic
Your code keeps clean camelCase names while the wire format keeps whatever the backend chose. Both sides win.
Snake Case Is Common
Many JSON APIs use snake_case, so mapping created_at to a createdAt property is one of the most frequent uses.
@SerialName("created_at") val createdAt: StringMap Reserved Words
If JSON has a key like type that clashes with your style, @SerialName frees you to name the property whatever reads best.
@SerialName("type") val kind: StringWorks on Every Field
You can annotate as many properties as you like, mixing renamed and default-named fields inside the same data class.
Rename Enum Entries Too
@SerialName also works on enum constants, so an ACTIVE entry can serialize as the lowercase string the API expects.
@SerialName("active") ACTIVEGlobal Naming Strategy
If every key is snake_case you can set a JsonNamingStrategy once instead of annotating each field by hand.
Json { namingStrategy = JsonNamingStrategy.SnakeCase }Annotation Wins
When both a global strategy and an explicit @SerialName apply, the annotation takes priority for that one field.
Same Class, Both Ways
The custom names apply to both encoding and decoding, so one annotated model handles reading and writing JSON.
Decoupled From the Backend
Mapping keys explicitly means a backend rename only touches one annotation, not your whole codebase.
Quick Check
How do you map a JSON key to a differently named Kotlin property?
Recap
Use @SerialName to map mismatched JSON keys to idiomatic Kotlin names on fields and enums, or set a global naming strategy for whole models.
คำถามที่พบบ่อย
บทเรียน “@SerialName และการจับคู่ฟิลด์แบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “@SerialName และการจับคู่ฟิลด์แบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “@SerialName และการจับคู่ฟิลด์แบบกำหนดเอง”
จับคู่ชื่อ Kotlin กับคีย์ JSON ที่ไม่ตรงกัน คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “@SerialName และการจับคู่ฟิลด์แบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โมเดลข้อมูล @Serializable
- @SerialName และการจับคู่ฟิลด์แบบกำหนดเอง
- ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม
- เชื่อม Serialization เข้ากับ Ktor