简体   繁体   English

使用Gson将XML文件转换为Json

[英]Convert an XML file to Json using using Gson

I found that many of the tutorials on the net and here on SO also refer to net.sf.json library to convert an xml file to json object. 我发现网上和SO上的许多教程也都引用net.sf.json库将XML文件转换为json对象。

But, I want an alternative preferably using Gson. 但是,我想要一个替代方案,最好使用Gson。 Is it possible? 可能吗? I don't have well defined Java Classes for the XML file. 我没有为XML文件定义好的Java类。 But, I just want to convert the xml file to com.google.gson.JsonObject . 但是,我只想将xml文件转换为com.google.gson.JsonObject How to achieve it? 如何实现呢?

I've done the same using JAXB to convert my xml to an object, and passing the object to gson. 我已经使用JAXB将xml转换为对象并将该对象传递给gson进行了相同的操作。 I know it takes one additional step, but that worked convenient for me. 我知道还需要采取其他步骤,但这对我来说很方便。 Upon converting xml to jaxb see also: Use JAXB to create Object from XML String 将xml转换为jaxb时,另请参见: 使用JAXB从XML字符串创建对象

Underscore-java can convert xml to json. Underscore-java可以将xml转换为json。 You may call U.xmlToJson(xml) method. 您可以调用U.xmlToJson(xml)方法。 I am the maintainer of the project. 我是该项目的维护者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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