json - 肥皂响应 XML 到 Json - 春季启动
问题描述
我在我的项目中使用 WSDL 服务,返回的响应是 XML。对于我的应用程序,我需要以 JSON 格式传输响应。这里使用的框架是spring boot。编码语言很时髦
我试图将 xml 转换为 Json,但不需要响应。
来自 WSDL 的 Soap 响应
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<RESP xmlns="http://xmlns.oracle.com/Enterprise/Tools/schemas/RESP">
<Data>
<Resp_Data>
<ID >123456789</ID>
<Name >Downey jr,Robert John</Name>
<LastName >Downey jr</LastName>
<FirstName >Robert</FirstName>
<MiddleName >John</MiddleName>
<Movies>
<MName>Sherlock Homes</MName>
<Year>2009</Year>
<Length>2h 8m</Length>
<BoxOffice>$208,711,166</BoxOffice>
<Ratings>
<ImdbRaring>7.6</ImdbRaring>
<RottenTomatoes>70%</RottenTomatoes>
</Ratings>
<Movies>
<Movies>
<MName>Iron Man</MName>
<Year>2008</Year>
<Length>2h 6m</Length>
<BoxOffice>$318,298,180</BoxOffice>
<Ratings>
<ImdbRaring>7.9</ImdbRaring>
<RottenTomatoes>93%</RottenTomatoes>
</Ratings>
<Movies>
<Movies>
<MName>Iron Man2</MName>
<Year>2008</Year>
<Length>2h 4m</Length>
<BoxOffice>$312,057,433</BoxOffice>
<Ratings>
<ImdbRaring>7.0</ImdbRaring>
<RottenTomatoes>73%</RottenTomatoes>
</Ratings>
<Movies>
</Resp_Data>
</Data>
<RESP>
</soapenv:Body>
</soapenv:Envelope>
我尝试使用 XML.toJSONObject(xmlString),转换后的 Json 响应是
{
"Envelope": {
"Body": {
"RESP": {
"Data": {
"Resp_Data": {
"ID": "123456789",
"Name": "Downey jr,Robert John",
"LastName": "Downey jr",
"FirstName": "Robert",
"MiddleName": "John",
"Movies": [
{
"MName": "Sherlock Homes",
"Year": "2009",
"Length": "2h 8m",
"BoxOffice": "$208,711,166",
"Ratings": {
"ImdbRaring": "7.6",
"RottenTomatoes": "70%"
}
},
{
"MName": "Iron Man",
"Year": "2008",
"Length": "2h 6m",
"BoxOffice": "$318,298,180",
"Ratings": {
"ImdbRaring": "7.9",
"RottenTomatoes": "93%"
}
},
{
"MName": "Iron Man2",
"Year": "2008",
"Length": "2h 4m",
"BoxOffice": "$312,057,433",
"Ratings": {
"ImdbRaring": "7.0",
"RottenTomatoes": "73%"
}
}
]
}
},
"_xmlns": "http://xmlns.oracle.com/Enterprise/Tools/schemas/RESP"
},
"__prefix": "soapenv"
},
"_xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
"_xmlns:soapenc": "http://schemas.xmlsoap.org/soap/encoding/",
"_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"__prefix": "soapenv"
}
}
另外,我尝试使用 groovy 的 Json Builder 来解析 XML 并创建 Json 格式。下面是代码
def responseXML = new XmlSlurper().parseText(xmlString)
def builder = new groovy.json.JsonBuilder()
builder {
responseXML.Body.RESP.Data.Resp_Data.each { nodeElem ->
nodeElem.childNodes().each { childElem ->
"$childElem.name" childElem.text()
}
}
}
我得到的 Json 响应是
{
"ID": "123456789",
"Name": "Downey jr,Robert John",
"LastName": "Downey jr",
"FirstName": "Robert",
"MiddleName": "John",
"W_CHKLST_WS_REC": "Sherlock Homes 2009 2h 8m $208,711,166 7.6 70% Iron Man 2008 2h 6m $318,298,180 7.9 93% Iron Man2 2008 2h 4m $312,057,433 7.0 73%"
}
我正在尝试实现 Json 响应为
{
"ID": "123456789",
"Name": "Downey jr,Robert John",
"LastName": "Downey jr",
"FirstName": "Robert",
"MiddleName": "John",
"Movies": [
{
"MName": "Sherlock Homes",
"Year": "2009",
"Length": "2h 8m",
"BoxOffice": "$208,711,166",
"Ratings": {
"ImdbRaring": "7.6",
"RottenTomatoes": "70%"
}
},
{
"MName": "Iron Man",
"Year": "2008",
"Length": "2h 6m",
"BoxOffice": "$318,298,180",
"Ratings": {
"ImdbRaring": "7.9",
"RottenTomatoes": "93%"
}
},
{
"MName": "Iron Man2",
"Year": "2008",
"Length": "2h 4m",
"BoxOffice": "$312,057,433",
"Ratings": {
"ImdbRaring": "7.0",
"RottenTomatoes": "73%"
}
}
]
}
解决方案
Underscore-java库可以将xml转换为json
import com.github.underscore.U;
public class JsonConversion {
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String args[]) {
String jsonPrettyPrintString = U.xmlToJson(TEST_XML_STRING);
System.out.println(jsonPrettyPrintString);
// {
// "test": {
// "-attrib": "moretest",
// "#text": "Turn this to JSON"
// }
// }
}
}
推荐阅读
- css - 如何在数据表分页中给出边距顶部?
- java - RuntimeException 序列版本 UID
- javascript - 如何检测 MaterializeCSS 模态关闭?
- android - 在android中的Imageview上应用缩放动画后如何保持图像质量?
- c++ - 从 unordered_map 内的向量中删除元素
- reactjs - 将社交登录数据存储在 Auth0 中的自定义 MongoDB 数据库中
- android - 当设备从广播接收器锁定时如何显示活动?
- c++ - 通过加法或减法使数组的所有元素相等
- reactjs - Expo 表单数据发送到 POST API
- node.js - 为什么我没有得到 mongodb 集合的响应?