DEV Community

loizenai
loizenai

Posted on

Kotlin - How to convert Object to/from JSON by Gson with Kotlin language

Kotlin - How to convert Object to/from JSON by Gson with Kotlin language

https://grokonez.com/kotlin/kotlin-convert-object-tofrom-json-gson-kotlin-language

This tutorial shows you how to use Gson to convert Kotlin object to/from JSON.

I. Technology

  • Java 1.8
  • Kotlin 1.1.2
  • Maven 3.5.1

    II. Overview

    1. Goal

  • Convert JSON string/JSON file to Person(name:String,age:Int,messages:List) Kotlin Object/List/Map
  • Convert Person Object/List/Map to JSON string.

    2. Steps to do

  • add Dependency:
    <dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>1.1.2</version>
    </dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>

  • import:
    
    import com.google.gson.Gson
    import com.google.gson.GsonBuilder
    import com.google.gson.reflect.TypeToken
    

More at:

https://grokonez.com/kotlin/kotlin-convert-object-tofrom-json-gson-kotlin-language

Top comments (0)