java JSONObject (org.json) 到 JsonElement(com.google.gson)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40654988/
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
JSONObject (org.json) to JsonElement(com.google.gson)
提问by ashutosh srivastava
I am writing a java code in which i need to convert a JSONObject to JsonElement.Is there any way using which i can convert a JSONObject (org.json) to JsonElement(com.google.gson)?
我正在编写一个 java 代码,我需要在其中将 JSONObject 转换为 JsonElement。有什么方法可以将 JSONObject (org.json) 转换为 JsonElement(com.google.gson)?
回答by fafl
One way that always works is to serialize the object to JSON and then parse it again:
一种始终有效的方法是将对象序列化为 JSON,然后再次解析它:
JSONObject myData = ...
Gson gson = new Gson();
JsonElement element = gson.fromJson(myData.toString(), JsonElement.class);