当然のことながら XML で応答を送信する SOAP Web サービスを使用する必要があります。私は 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.Body.linkAppResponse.result.text の代わりにクエリが成功したかどうかを確認するために、単純に json.result を呼び出すことができます。
どんな助けでも大歓迎です。