简体   繁体   中英

How can I convert a JSONObject to a gson.JsonObject?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM