How to convert HashMap to JSON in Kotlin
Use kotlinx.serialization:
import kotlinx.serialization.*
import kotlinx.serialization.json.*
fun main() {
var store = HashMap<String, String>()
var jsonString= Json.encodeToString(store)
var anotherStore = Json.decodeFromString(jsonString)
}
You can use org.json
which is shipped with Android:
JSONObject(map).toString()
You can use Gson for that,
Here is the example,
val map = HashMap<String, String>()
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
val gson = Gson()
Log.d("TAG", gson.toJson(map).toString())
and the oputput is,
{"key1":"value1","key2":"value2","key3":"value3"}