[英]Best way of handling string response from soap web service call in node.js
新手問題在這里。 我正在調用使用node-soap在node.js中進行Web服務調用,並得到類似於以下內容的響應:
{ AuthenticateResult:
{ PrimaryKeyId: '0',
ValidateOnly: false,
OperationResult: 'Succeeded',
SessionId: 'abc45235435345' } }
如果OperationResult為“成功”,從響應中提取SessionId值的最佳方法是什么? 我猜我可以用indexof和substring做到這一點,但是即使對於像我這樣的新手,這聽起來也不是一個很好的解決方案。
假設輸入存儲為字符串,並且是一致的(即JSON,冒號左側不帶引號),則可以使用正則表達式將其轉換為JSON字符串, 然后使用JSON.parse()
。
var input = "{ AuthenticateResult: {\\n" + "PrimaryKeyId: '0',\\n" + "ValidateOnly: false,\\n" + "OperationResult: 'Succeeded',\\n" + "SessionId: 'abc45235435345' } }"; // This replaces word: with "word": and ' with " var json = input.replace(/(\\w+):/g, '"$1":').replace(/'/g, '"'); // This here's the object you want var obj = JSON.parse(json); // Just printing out the JSON so you know it works. document.getElementById('result').innerHTML = json;
<pre id="result"></pre>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.