2013-06-26 87 views
3

由于我正在开发Appcelerator Titanium移动应用程序,因此我需要使用SOAP Web服务,该服务自然会以XML格式发送其响应,因此我更愿意使用JSON中的响应。网上通缉后,我转换使用this Javascript代码的响应,它主要工作,但返回的结果如下所示:对JSON的SOAP响应(XML)

{ 
    "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

任何帮助,非常感谢。

+0

JSON不使用'key = value',它使用'key:value'。这些只是例子吗?对不起,我没有阅读你的整篇文章 – Ian

+0

当我在返回的JSON对象上调用console.debug()时,它们就是记录到控制台的。我注意到它不是很正确,但我认为这是使用了tostring函数的结果,我会调整我的问题,谢谢! – JonoCoetzee

回答

6

想出了一个可行的解决方案,但不是那么肮脏,但它工作并以我想要的格式返回数据。

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"; 
} 

我要离开这个问题开了而如果有人提出更好的解决方案。