2013-06-26 83 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"; 
} 

我要離開這個問題開了而如果有人提出更好的解決方案。