ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม
จัดการ JSON ที่ขาดหายหรือมีข้อมูลบางส่วนอย่างเหมาะสม
ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
JSON Is Often Incomplete
Real responses skip fields or send null. Your shared models must handle missing data without crashing either app.
Make a Field Nullable
Mark a property as nullable with a trailing question mark, and the serializer happily reads a JSON null into it.
@Serializable
data class User(val bio: String?)Null vs Missing
A key set to null and a key that is simply absent are different cases, and the library treats each one distinctly.
Give a Default Value
A property with a default can be left out of the JSON entirely, and decoding fills in your fallback.
val role: String = "member"Optional Equals Default
In kotlinx.serialization, an optional field just means one that has a default, so absence is never an error.
Required Fields Are Strict
A non-null property with no default is required. If the JSON omits it, decoding throws a clear exception.
Nullable With a Default
You can combine both: a nullable property that defaults to null tolerates a missing key and a null value alike.
val avatar: String? = nullSkip Null on Output
Set explicitNulls to false and the encoder omits null fields instead of writing them out as null.
Json { explicitNulls = false }Tolerate Extra Keys
APIs add fields over time. Turn on ignoreUnknownKeys so new JSON keys never break your existing models.
Json { ignoreUnknownKeys = true }Coerce Bad Values
With coerceInputValues, a null sent for a field that has a default falls back to that default instead of failing.
Json { coerceInputValues = true }Resilient by Design
Combining nullable types, defaults and lenient config makes your shared parsing robust against messy real-world JSON.
Quick Check
How do you let a JSON key be safely omitted from a response?
Recap
Use nullable types and defaults for missing data, and tune Json with ignoreUnknownKeys, explicitNulls and coerceInputValues for resilient parsing.
คำถามที่พบบ่อย
บทเรียน “ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม”
จัดการ JSON ที่ขาดหายหรือมีข้อมูลบางส่วนอย่างเหมาะสม คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โมเดลข้อมูล @Serializable
- @SerialName และการจับคู่ฟิลด์แบบกำหนดเอง
- ฟิลด์ที่เป็นค่า null ค่าเริ่มต้น และฟิลด์เสริม
- เชื่อม Serialization เข้ากับ Ktor