From the course: Android Development Essential Training: Manage Data with Kotlin

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Map JSON and class property names

Map JSON and class property names

From the course: Android Development Essential Training: Manage Data with Kotlin

Start my 1-month free trial

Map JSON and class property names

- [Narrator] My app is successfully parsing a JSON file's contents into a list of data objects in memory. This is working now, because my class properties and the names of the properties in the JSON file match each other. But if there's a mismatch, you can handle it using annotations. I'll go to my monster.kt file, and I'm going to refactor this property called MonsterName. I'll press Shift + F6, and then I'll change this from MonsterName to simply Name. And that should also change it here for where I'm referencing the property in the view model class. Now, I'm going to run the code and look at Logcat again. I'll clear the Logcat and then show the new result, and I see that the values for the MonsterName come in as null. And this is what happens automatically when you have a property name mismatch. Next, I'll go back to the monster class, and I'm going to add an annotation simply called JSON. And this comes from the Moshi library. And I'll pass in a property called Name, and I'll set…

Contents