Mohsin
Mohsin

Reputation: 55

Converting an JSON string to Gson object

I have an JSONObject which is creating in the following way. For a special reason, I have to convert it to Gson in this way, as i can modify only extractDataToString() method. But I do not get proper value after conversion.

fun createJsonString() {
    val headerJson = JSONObject()
    headerJson.put("typ1", "value1")
    headerJson.put("typ2", "value2")
    extractData(headerJson.toString())
} 

fun extractDataToString(jsonString: String) {
    val headerJson = Gson().fromJson(jsonString, JsonObject::class.java)
    val resultType1 = headerJson.remove("typ1")?.toString() // Here, resultType1 becomes 
                                                                "value1", but it should be 
                                                                 value1 that is extra qutomation 
                                                                 mark is being added.
}

Why extra quotation mark is being added? Can anyone please help? I am a newbie.

Upvotes: 1

Views: 238

Answers (1)

Vojin Purić
Vojin Purić

Reputation: 2385

Call

.getAsString()

Instead of

toString()

at the end ref : docs

Upvotes: 2

Related Questions