[英]Soap Response XML to Json - spring boot
我在我的項目中使用 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.lodash.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"
// }
// }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.