GSON Library allows both way of parsing.
1. From JSON to Model/Data Class ( JSON > Model/Data )
2. Model/Data to JSON ( Model/Data > JSON )
Gradle
Add GSON dependency to app level build.gradle filedependencies { ............ ............ implementation 'com.google.code.gson:gson:2.8.5' }
1.a Simple JSON
In this tutorial we are reading file from assets folder of android
private fun readFromAssets(fileName: String): String { val bufferReader = application.assets.open(fileName).bufferedReader() return bufferReader.use { it.readText() } }
file : app/src/main/assets/sample.json
{ "name": "John", "age": 29 }
Model /Data Class
file : Model.kt
package com.tutorialsbuzz.androifromgson import com.google.gson.annotations.SerializedName data class Model( @SerializedName("name") val name: String, @SerializedName("age") val age: Int ) {}
Parsing
private fun fromJSimpleSONToModel(): Unit { val response = readFromAssets("sample.json") val gson = Gson() val model: Model = gson.fromJson(response, Model::class.java) Log.d("fromJSimpleSONToModel: ", "${model.name},${model.age}") }
Log-cat :
com.tutorialsbuzz.androifromgson D/fromJSimpleSONToModel:: John,29
1.b ArrayOfJSONObject
file : app/src/main/assets/country.json
[ { "name": "India", "capital": "Delhi" }, { "name": "United States of America", "capital": "Washington D C" }, { "name": "United Kingdom", "capital": "London" }, { "name": "Australia", "capital": "Canbera" }, { "name": "France", "capital": "Paris" }, { "name": "Germany", "capital": "Berlin" }, { "name": "Russia", "capital": "Moscow" }, { "name": "Brazil", "capital": "Brasilia" }, { "name": "Ukraine", "capital": "Kyiv" }, { "name": "Kenya", "capital": "Nairobi" }, { "name": "Switzerland", "capital": "Bern" }, { "name": "Sweden", "capital": "Stockholm" }, { "name": "New Zealand", "capital": "Wellington" } ]
Model/Data Class
file : Country.kt
package com.tutorialsbuzz.androifromgson import com.google.gson.annotations.SerializedName data class Country( @SerializedName("name") val name: String, @SerializedName("capital") val capital: String ) {}
Parsing
private fun fromArrayOfJSONToModel(): Unit { val response = readFromAssets("country.json"); val gson = Gson() val countryList: List<Country> = gson.fromJson(response, Array<Country>::class.java).toList() for (country: Country in countryList) { Log.d("fromArrayOfJSONToModel", "${country.name} , ${country.capital} \n") } }
Log-cat:
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: India , Delhi
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: United States of America , Washington D C
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: United Kingdom , London
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: Australia , Canbera
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: France , Paris
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: Germany , Berlin
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: Russia , Moscow
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: Brazil , Brasilia
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: Ukraine , Kyiv
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: Kenya , Nairobi
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: Switzerland , Bern
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: Sweden , Stockholm
com.tutorialsbuzz.androifromgson D/fromArrayOfJSONToModel: New Zealand , Wellington
No comments:
Post a Comment