SOAP对JSON的响应(XML)
SOAP response (XML) to JSON
我需要使用SOAP web服务,它自然会以XML发送响应,因为我正在开发Appcelerator Titanium移动应用程序,所以我更喜欢JSON格式的响应。在线查看后,我使用此Javascript代码转换了响应,它基本上起作用,但返回了以下结果:
{
"SOAP-ENV:Body" : {
"ns1:linkAppResponse" : {
"ns1:result" : {
#text : true;
};
"ns1:uuid" : {
#text : "a3dd915e-b4e4-43e0-a0e7-3c270e5e7aae";
};
};
};
}
当然,中的冒号和散列引起了问题,所以我调整了代码,在名称上添加了一个子字符串,并删除了":"之前的任何内容,然后对生成的JSON进行了字符串化,删除了所有散列,并再次解析了JSON。这对我来说有点乱,但我最终得到了一些有用的东西。
以下是我正在使用的xmlToJson代码:
// Changes XML to JSON
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) {// element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) {// text
obj = xml.nodeValue;
}
// do children
if (xml.hasChildNodes()) {
for (var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName.substring(item.nodeName.indexOf(":") + 1);
if ( typeof (obj[nodeName]) == "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if ( typeof (obj[nodeName].push) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
};
module.exports = xmlToJson;
这导致了以下JSON:
{
Body : {
linkAppResponse : {
result : {
text : true;
};
uuid : {
text : "9022d249-ea8a-47a3-883c-0f4cfc9d6494";
};
};
};
}
虽然这返回了一个我可以使用的JSON对象,但我更希望得到以下形式的JSON:
{
result : true;
uuid : "9022d249-ea8a-47a3-883c-0f4cfc9d6494";
};
大多数情况下,它不那么冗长,我可以简单地调用json.result来检查查询是否成功,而不是json。Body.linkAppResponse.result.text
非常感谢您的帮助。
提出了一个有效的解决方案,虽然不那么脏,但它可以工作,并以我想要的格式返回数据。
function soapResponseToJson(xml) {
var json = xmlToJson(xml).Body;
console.debug(json);
var response = {};
for (var outterKey in json) {
if (json.hasOwnProperty(outterKey)) {
temp = json[outterKey];
for (var innerKey in temp) {
if (temp.hasOwnProperty(innerKey)) {
response[innerKey] = temp[innerKey].text;
}
}
}
}
console.debug(response);
return response;
}
// Changes XML to JSON
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) {// element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) {// text
obj = xml.nodeValue;
}
// do children
if (xml.hasChildNodes()) {
for (var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName.substring(item.nodeName.indexOf(":") + 1).replace('#', '');
if ( typeof (obj[nodeName]) == "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if ( typeof (obj[nodeName].push) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
};
module.exports = soapResponseToJson;
console.debug(json):
{
linkAppResponse : {
result : {
text : true;
};
uuid : {
text : "e4f78c5f-1bc2-4b50-a749-19d733b9be3f";
};
};
}
console.debug(响应):
{
result : true;
uuid : "e4f78c5f-1bc2-4b50-a749-19d733b9be3f";
}
我将暂时不提这个问题,以防有人想出更好的解决方案。
我觉得这是一个相当丑陋的解决方案(希望它不会冒犯您:)。为什么不将xml封送到一个对象,然后使用gson或jackson映射到json呢。我不知道您使用的是什么框架,例如在春季,您可以使用jaxb2进行封送,使用jackson或gson将对象转换为json。
相关文章:
- 显示IIS上javascript文件(SOAP请求)的XML响应
- XMLHttpRequest获取请求xml响应
- dojo/request/iframe中读取xml响应的缺陷
- 跨域 Post 方法 ajax 调用使用 jQuery 和 XML 响应
- Angularjs - 使用 $http 获取 xml 响应而不是 json
- 哪个“控制器”在 JSON 中转换来自 .NET Web 服务的 XML 响应
- AJAX 检索 xml 响应
- sinon fakeserver 可以发送 xml 响应吗?
- 如何使用 jquery 通过函数解析 XML 响应
- 可以在ExtJS中将XML响应作为流读取吗
- 如何使用JspPage获取xml响应
- XHR纯XML响应类型
- IE9拒绝处理XML响应
- 来自thetvdb的Angular js和XML响应
- ExtJS 5中表单提交的XML响应出错
- 在ajax xml响应中正确检测数字数据
- 如何在JavaScript中解析API的XML响应
- 需要使用Javascript/JQuery使用Rest API XML响应
- 使用jquery获取跨站点XML响应
- Internet explorer截获XML响应