Java – how to convert jsonobject to gson JsonObject?

I have an org json. Jsonobject object

Create gson. From What is the simplest method of jsonobject object?

thank you

Solution

The simplest method is to serialize jsonobject into a JSON string using tostring(), and then parse the JSON string into jsonobject:

org.json.JSONObject object = <your defined object>;
    JsonParser jsonParser = new JsonParser();
    JsonObject gsonObject = (JsonObject)jsonParser.parse(object.toString());

Note that serializing and deserializing objects can be an expensive operation If you have to do this in code (internal loop), it may affect your performance

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>