JSONObject(org.json)到JsonElement(com.google.gson) [英] JSONObject (org.json) to JsonElement(com.google.gson)

查看:157
本文介绍了JSONObject(org.json)到JsonElement(com.google.gson)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java代码,需要将JSONObject转换为JsonElement.是否可以通过任何方式将JSONObject(org.json)转换为JsonElement(com.google.gson)?

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)?

推荐答案

始终有效的一种方法是将对象序列化为JSON,然后再次对其进行解析:

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

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

这篇关于JSONObject(org.json)到JsonElement(com.google.gson)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆