java 如何将 JSONObject 转换为 gson.JsonObject?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31866834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert a JSONObject to a gson.JsonObject?
提问by special0ne
I have a org.json.JSONObject
object.
我有一个org.json.JSONObject
对象。
What's the easiest way to create a gson.JsonObject
object from it?
从中创建gson.JsonObject
对象的最简单方法是什么?
Thanks
谢谢
回答by Scott Schechter
The easiest way is to serialize your JSONObject to a json string using toString(), then parsing that json string into a JsonObject:
最简单的方法是使用 toString() 将 JSONObject 序列化为 json 字符串,然后将该 json 字符串解析为 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.
请注意,序列化和反序列化对象可能是一项昂贵的操作。如果您必须在代码中(在循环内)多次执行此操作,则会影响您的性能。