[Android] Convert Map to JSON using GSON in Kotlin and Java

Introduction

GSON is a JSON parsing library provided by Google. You can convert Java objects and JSON, but Kotlin can. This time, I could easily convert it with Kotlin's MutableMap, so I would like to share it.

How to use GSON

Write the following in Android Studio.

build.gradle


dependencies {
    implementation 'com.google.code.gson:gson:*.*.*'
}

"*. *. *" Describe the latest version. Check the latest version from the following. https://github.com/google/gson

After writing the above, click "Sync Now" on Android Studio and you're ready to go.

スクリーンショット 2018-07-03 3.12.37.png

GSON is now available in your project.

JSON conversion of Java Map

I implemented the JSON conversion process as follows.

Sample.java


public void test(){
    ArrayMap<String, String> map = new ArrayMap<>();
    map.put("a1", "A1");
    map.put("a2", "A2");
    map.put("a3", "A3");

    Gson gson = new Gson();
    String jsonString = gson.toJson(map);
    Log.d("Java", jsonString);

    Type type = new TypeToken<ArrayMap<String, String>>(){}.getType();
    ArrayMap<String, String> map2 = gson.fromJson(jsonString, type);
    for (String mapValue : map2.values()) {
        Log.d("Java", mapValue);
    }
}

When you check the log

D/Java: {"a1":"A1","a2":"A2","a3":"A3"} D/Java: A1   A2   A3

become that way. It was confirmed that JSON conversion is possible. Up to this point, it's been done before, so there's nothing new.

JSON conversion of Kotlin Map

The main subject is from here. Implement the above Java implementation in Kotlin. Map uses Kotlin's MutableMap.

Sample.kt


fun test(){
    val map : MutableMap<String, String> = mutableMapOf()
    map.put("a1", "A1")
    map.put("a2", "A2")
    map.put("a3", "A3")

    val gson = Gson()
    val jsonString : String = gson.toJson(map)
    Log.d("Kotlin", jsonString)
    val type : Type = object : TypeToken<MutableMap<String, String>>() {}.type

    val map2 : MutableMap<String, String> = gson.fromJson(jsonString, type)
    for (mapValue in map2.values) {
        Log.d("Kotlin", mapValue)
    }
}

When you check the log

D/Kotlin: {"a1":"A1","a2":"A2","a3":"A3"} D/Kotlin: A1      A2      A3

become that way. It was confirmed that JSON conversion can be performed in the same way as Java.

At the end

The ease of use of Kotlin is GOOD!

Recommended Posts

[Android] Convert Map to JSON using GSON in Kotlin and Java
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
How to convert A to a and a to A using AND and OR in Java
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
[Android] Convert Android Java code to Kotlin
Java to C and C to Java in Android Studio
Convert Java enum enums and JSON to and from Jackson
Refer to C ++ in the Android Studio module (Java / kotlin)
[Java] Convert 1-to-N List to Map
How to POST JSON in Java-Method using OkHttp3 and method using HttpUrlConnection-
Map without using an array in java
Convert SVG files to PNG files in Java
Convert JSON to TSV and TSV to JSON with Ruby
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
Create API using Retrofit2, Okhttp3 and Gson (Java)
Kotlin functions and lambdas to send to Java developers
Code to escape a JSON string in Java
[Java] Convert DB code to code value using enum
Represents "next day" and "previous day" in Java / Android
How to write Java String # getBytes in Kotlin?
Summary of good points and precautions when converting Java Android application to Kotlin
Kotlin Class to send to Java developers
[Android] Convert Android Java code to Kotlin
Impressions and doubts about using java for the first time in Android Studio
Try to realize Scala-like Option type pattern matching and map, flatMap in Java
Read JSON in Java
[Android / Java] Screen transition and return processing in fragments
<java> Read Zip file and convert directly to string
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 3
Sample code to convert List to List <String> in Java Stream
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 2
[Java] How to get the key and value stored in Map by iterative processing
Write a class in Kotlin and call it in Java
Java classes and instances to understand in the figure
POST JSON in Java
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 1
I want to transition screens with kotlin and java!
How to convert a file to a byte array in Java
Create JSON in Java
What Android, Kotlin beginners did in a month and what they were able to do
Mutual conversion between Java objects and JSON using Moshi
Gzip-compress byte array in Java and output to file
Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK
Store in Java 2D map and turn with for statement
JSON in Java and Jackson Part 1 Return JSON from the server
How to use JSON data in WebSocket communication (Java, JavaScript)
[Java] Change language and locale to English in JVM options
I tried to summarize the basics of kotlin and java
Convert a Java byte array to a string in hexadecimal notation
[Android, Java] Convenient method to calculate the difference in days
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
What happened in "Java 8 to Java 11" and how to build an environment
How to call and use API in Java (Spring Boot)
Compared processing time to convert Apache Arrow and JSON to Yosegi
Create API to send and receive Json data in Spring
You are currently using Java 6. Solution in Android Studio Gradle
Reasons to use Servlet and JSP separately in Java development
I tried to convert a string to a LocalDate type in Java
I tried using Dapr in Java to facilitate microservice development