繁体   English   中英

对JSON的SOAP响应(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";
}

如果有人想出更好的解决方案,我将暂时搁置这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM