Kotlin for Android Developers
Mapping an object into variables
Download 1.04 Mb. Pdf ko'rish
|
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )
- Bu sahifa navigatsiya:
- 10 Parsing data 10.1 Converting json to data classes
9.3 Mapping an object into variables
This process is known as multi-declaration and consists of mapping each property inside an object into a variable. That’s the reason why the componentX functions are automatically created. An example with the previous Forecast class: 1 val f1 = Forecast(Date(), 27.5f, "Shiny day") 2 val (date, temperature, details) = f1 This multi-declaration is compiled down to the following code: 1 val date = f1.component1() 2 val temperature = f1.component2() 3 val details = f1.component3() The logic behind this feature is very powerful, and can help simplify the code in many situations. For instance, Map class has some extension functions implemented that allow to recover its keys and values in an iteration: 1 for ((key, value) in map) { 2 Log.d("map", "key:$key, value:$value") 3 } 10 Parsing data 10.1 Converting json to data classes Now that we know how to create data classes, we are ready to start parsing data. In the data package, create a new file called ResponseClasses.kt . If you open the url we used in chapter 8, you can see the structure of the json file. It basically consists of an object which contains a city, and a list of forecast predictions. The city has an id, a name, its coordinates and the country it belongs to. Each forecast comes with a good set of information such as the date, different temperatures, and a weather object with the description and an id for an icon, for instance. In our current UI we are not going to use all this data. However, we’ll parse everything down to classes, in case it is of some use in the future. These are the data classes we need: 1 data class ForecastResult(val city: City, val list: List 2 3 data class City(val id: Long, val name: String, val coord: Coordinates, 4 val country: String, val population: Int) 5 6 data class Coordinates(val lon: Float, val lat: Float) 7 8 data class Forecast(val dt: Long, val temp: Temperature, val pressure: Float, 9 val humidity: Int, val weather: List 10 val speed: Float, val deg: Int, val clouds: Int, 11 val rain: Float) 12 13 data class Temperature(val day: Float, val min: Float, val max: Float, 14 val night: Float, val eve: Float, val morn: Float) 15 16 data class Weather(val id: Long, val main: String, val description: String, 17 val icon: String) As we are using Gson to parse the json to our classes, the properties must have the same name as the ones in the json, or specify a serialised name. A good practice explained in most software architectures is to use different models for the different layers in our app to decouple them from each other. So I prefer to simplify the declaration of these classes, because I’ll convert them before being used in the rest of the app. The names of the properties here are exactly the same as the names in the json response. 33 10 Parsing data 34 Now, the Request class needs some modifications in order to return the parsed result. It will also receive only the zipcode of the city instead of the complete url, so that it becomes more readable. For now, the static url will belong to a companion object. Maybe we need to extract it later if we create more requests against another endpoint of the API. Download 1.04 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling