JSONObject (org.json) in JsonElement (com.google.gson)

I am writing Java code in which I need to convert a JSONObject to JsonElement.Is there any way with which I can convert a JSONObject (org.json) to JsonElement (com.google.gson)?

+4
source share
1 answer

One way that always works is to serialize the object in JSON and then parse it again:

JSONObject myData = ...
Gson gson = new Gson();
JsonElement element = gson.fromJson(myData.toString(), JsonElement.class);
+9
source

Source: https://habr.com/ru/post/1661053/


All Articles