I have a org.json.JSONObject
object.
What's the easiest way to create a gson.JsonObject
object from it?
Thanks
I have a org.json.JSONObject
object.
What's the easiest way to create a gson.JsonObject
object from it?
Thanks
The easiest way is to serialize your JSONObject to a json string using toString(), then parsing that json string into a JsonObject:
org.json.JSONObject object = <your defined object>;
JsonParser jsonParser = new JsonParser();
JsonObject gsonObject = (JsonObject)jsonParser.parse(object.toString());
Note that serializing and deserializing an object can be an expensive operation. If you have to do this a lot in your code (inside loops), it can affect your performance.
JSONObject jsonObject = new JSONObject(jsonObject.toString());
–
Hydatid in Kotlin use this extension:
fun JSONObject.toGsonJsonObject():JsonObject{
return JsonParser().parse(this.toString()) as JsonObject
}
JsonParser().parse(String)
is deprecated functions instead use below
var object:JSONObject = {"key":"value"}
var json :JsonObject = JsonParser.parseString(object.toString()).asJsonObject
© 2022 - 2024 — McMap. All rights reserved.