2012-05-02 50 views
0

我有這個字符串,我需要使用JavaScript重新構造。在javascript中重構字符串

標籤= 11121212 & TopicArn =測試& AWSAccountId.member.1 = YYYYYYY & ActionName.member.1 = createTopic &動作= AddPermission &版本= 2010-03-31 & AWSAccessKeyId = XXXXXXXXX & SignatureVersion = 2 &是SignatureMethod = HMACSHA1 &時間戳= 2012-05-02T16%3A06%3A09.000Z &簽名= C3uIh%2BZ%2Fik

在這個例子中,AWSAccessKeyId應該是字符串的第一部分,label應該是第二個字符。還有其他的,與此類似。

預期輸出--AWSAccessKeyId = XXXXXXXXX & AWSAccountId.member.1 = YYYYYYYYY &動作= AddPermission & ActionName.member.1 =發佈&標籤= IOS-SNS-權限標籤&簽名= dEaNL0ibP5c7xyl4qXDPFPADW0meoUX9caKyUIx1wkk%3D &是SignatureMethod = HmacSHA256 & SignatureVersion = 2 &時間戳= 2012-05-02T00%3A51%3A23.965Z & TopicArn = ARN%3Aaws%3Asns%3Aus - 東 - 1%3A335750469596%3AiOSGoesWooooo-1335919882 &版本= 2010-03-31

創建這個錯誤的代碼t字符串

exports.generatePayload = function(params, accessKeyId, secretKey, endpoint) { 
    var host = endpoint.replace(/.*:\/\//, ""); 
    var payload = null; 

    var signer = new AWSV2Signer(accessKeyId, secretKey); 
    params = signer.sign(params, new Date(), { 
     "verb" : "POST", 
     "host" : host, 
     "uriPath" : "/" 
    }); 

    var encodedParams = []; 
    for(var key in params) { 
     if(params[key] !== null) { 
      encodedParams.push(encodeURIComponent(key) + "=" + encodeURIComponent(params[key])); 
     } else { 
      encodedParams.push(encodeURIComponent(key)); 
     } 
    } 
    payload = encodedParams.join("&"); 
    return payload; 
} 

我試着把這個放在一個數組中,並重新構造它,但它並沒有爲我工作。

請指點如何能輕鬆地使用JavaScript

+1

進行顯示你嘗試過什麼 –

+0

當你說你試圖把它在一個數組和調整你的意思是你的代碼?你可以發佈代碼嗎?這應該工作。 – dweiss

+0

所以我有和數組包含字符串的所有元素,我做一個連接,以便像這樣一個字符串--- d = []; d.push(「label = 11111」); \t return e = d.join(「&」) – Amit

回答

1
exports.generatePayload = function(params, accessKeyId, secretKey, endpoint) { 
    var host = endpoint.replace(/.*:\/\//, ""); 
    var payload = null; 

    var signer = new AWSV2Signer(accessKeyId, secretKey); 
    params = signer.sign(params, new Date(), { 
     "verb" : "POST", 
     "host" : host, 
     "uriPath" : "/" 
    }); 

    var encodedParams = []; 
    if(params["AWSAccessKeyId"] != null) 
    { 
     encodedParams.push(encodeURIComponent("AWSAccessKeyId") + "=" + encodeURIComponent(params["AWSAccessKeyId"])); 

    } 
    if(params["AWSAccountId.member.1"] != null) 
    { 
     encodedParams.push(encodeURIComponent("AWSAccountId.member.1") + "=" + encodeURIComponent(params["AWSAccountId.member.1"])); 

    } 
    //other_ifs_for_param_keys_here 
    payload = encodedParams.join("&"); 
    return payload; 
+0

有更有效的方法可以做到這一點,而不是使用任何循環,然後它將按照你聲明的順序檢查它。這個,但是這會起作用。 – dweiss