kotlin json

// JSON Kotlin with GSON
// Maven declare in pom.xml:
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>
// Or Gradle / Android
dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}
  
data class TestModel(
    val id: Int,
    val description: String
)

// from Data Class to JSON String    json: {"id":1,"description":"Test"}
var gson = Gson()
var jsonString = gson.toJson(TestModel(1,"Test"))

// from JSON String to a Data Class
var jsonString = """{"id":1,"description":"Test"}""";
var testModel = gson.fromJson(jsonString, TestModel::class.java)

4
5

                                    import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
class MyJsonClass(val name: String, val optionalInt: Int = 5)

fun main() {
  val jsonToEncode = MyJsonClass( &quot;Alice&quot;, 10 )
  val encodedJson  = Json.encodeToString( jsonToEncode )
  val decodedJson  = Json.decodeFromString&lt;MyJsonClass&gt;( jsonString )
}

4 (5 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
any to json kotlin kotlin get json how to create a json object in kotlin object from json string kotlin kotlin to json string de json a kotlin create json in kotlin kotlin js json json for kotlin kotlin read json object kotlin org.json.jsonobject kotlin jsonobject json object kotlin kotlin create json object jsonobject kotlin example kotlin add json json response in kotlin HOW TO create a json in kotlin what is the json object in kotlin kotlin make json call kotlin json class create json object with kotlin kotlin json file kotlin json model working with json in kotlin json to kotlin object json object in kotlin how to get json object in kotlin kotlin jsonproperty kotlin jason kotlin object json kotlin json read kotlin to json KOTLIN jsonp ({ kotlin json element kotlin json library json kotlin kotlin json object extract kotlin json object get kotlin org.json kotlin jsonobject ktor kotlin read json kotlin org json jsonobject for Object from json in kotlin kotlin code generate from json kotlin json string create json kotlin json example in kotlin kotlin json object does kotlin have JSON jsonobject kotlin what is json in kotlin json api kotlin jsonobject in kotlin kotlin convert jsonobject to array kotlin parse json json in kotlin json to kotlin kotlin json like syntax convert string to object in kotlin json array of json objects android kotlin android kotlin object json to array access data from json object kotlin does kotlin use jason kotlin access json property parse json with kotlin kotlin predified json to unpredified kotlin json
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source