如何在Java中将XML转换为JSON? [英] how to convert XML to JSON in java?
本文介绍了如何在Java中将XML转换为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Java servlet中将XML转换为JSON.
How to convert XML to JSON in java servlet.
<?xml><SOAP-ENV:Envelope xmlns:xsd= "http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:HNS="http://tempuri.org/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header><HNS:ROClientID SOAP-ENV:mustUnderstand="0">{6C9A8E69-2018-4090-8FA7-DEB98300E102}</HNS:ROClientID></SOAP-ENV:Header><SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ro="http://tempuri.org/"><NS1:GetStationListResponse xmlns:NS1="urn:WOOSServices-WOrbitService"><Stations xsi:type="xsd:string"></Stations><Result xsi:type="xsd:string">{
"MOColmns": [
{
"MOTitle": "Description"
},
{
"MOTitle": "station_name"
},
{
"MOTitle": "StationID"
},
{
"MOTitle": "StationINT"
}
]
}</Result></NS1:GetStationListResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>";
String xml = "<xx yy='nn'><mm>zzz</mm></xx>";
JSONArray json = (JSONArray) XMLSerializer.read(xml);
System.out.println( json );
请帮助我.
推荐答案
在那里,您可以找到XML和JSONObject类.该代码可以为您工作:
There you can find the XML and JSONObject classes, among others. This code could work for you:
public String XMLtoJSON(String xml) {
JSONObject jsonObj = XML.toJSONObject(xml);
String json = jsonObj.toString();
return json;
}
这篇关于如何在Java中将XML转换为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文