0
上午角4個初學者,我沒有足夠的知識,如何處理XML到JSON以及如何調用服務的角度4,請根據圖書館http://goessner.net/download/prj/jsonxml/建議如何將xml轉換爲角4中的json?
上午角4個初學者,我沒有足夠的知識,如何處理XML到JSON以及如何調用服務的角度4,請根據圖書館http://goessner.net/download/prj/jsonxml/建議如何將xml轉換爲角4中的json?
,我實現了一個樣本接收XML數據並分析它們到Angular4應用:
var headers = new Headers();
(...)
headers.append('Accept', 'application/xml');
return this.http.get('https://angular2.apispark.net/v1/companies/', {
headers: headers
}).map(res => JSON.parse(xml2json(res.text(),' ')));
爲了能夠使用圖書館,你需要首先解析XML字符串:
var parseXml;
if (typeof window.DOMParser != "undefined") {
parseXml = function(xmlStr) {
return (new window.DOMParser()).parseFromString(xmlStr, "text/xml");
};
} else if (typeof window.ActiveXObject != "undefined" &&
new window.ActiveXObject("Microsoft.XMLDOM")) {
parseXml = function(xmlStr) {
var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlStr);
return xmlDoc;
};
} else {
throw new Error("No XML parser found");
}
看到這個plunkr:https://plnkr.co/edit/dj63ASQAgrNcLLlwyAum?p=preview。
謝謝。這個轉換我得到它,仍然混淆瞭如何轉換我們的ts(打字稿)文件(example.ts)文件。 –
不要混淆,只是顯示plunkr的例子,看你如何在我們的ts文件中轉換 –
很好! @FerasAlSous –