2012-09-11 79 views
0

我有一個複雜的JSON對象是這樣的:循環將數據添加到複雜的JSON對象

var requestData = { __batchRequests: [ { __changeRequests: [ 
{ requestUri: "Customers", method: "POST", headers: { "Content-ID": "1" }, data: { 
    CustomerID: 400, CustomerName: "John" 
} } 
] } ] };   

我試圖做兩件事情:

  1. 聲明此對象,但與變量數據

  2. 通過循環,動態添加項目到數據對象,

我該怎麼辦?

回答

1

這並不是太複雜的對象。直到它被轉換成一個字符串,它纔是JSON。 現在,它只是普通的'JS對象和數組。

打破下來到它的元素可能是這樣的:

var requestData = {}; 
requestData.__batchRequests = []; 
requestData.__batchRequests[0] = {}; 
requestData.__batchRequests[0].__changeRequests = []; 
requestData.__batchRequests[0].__changeRequests[0] = {}; 
requestData.__batchRequests[0].__changeRequests[0].requestUri = "Customers"; 
requestData.__batchRequests[0].__changeRequests[0].method = "POST"; 
requestData.__batchRequests[0].__changeRequests[0].headers = { "Content-ID" : "1" }; 
requestData.__batchRequests[0].__changeRequests[0].data = {}; 
從重複

除此之外,你怎麼看? 就個人而言,我看到__changeRequests[0]是一個對象一樣簡單:

var changeRequest = { 
    requestUri : "Customers", 
    method : "POST", 
    headers : { "Content-ID" : "1" }, 
    data : {} 
}; 

我也看到,我只能說推到我的變更請求的數組:

requestData.__batchRequests[0].__changeRequests.push(changeRequest); 

,對嗎?

我也知道,我changeRequest變量仍指向我剛添加到陣列中的一個,並且無論我改變的對象將顯示爲在陣列的參考對象改變了,太:

changeRequest.data.CustomerName = "Bob"; 
changeRequest.data.CustomerID = "204"; 


requestData.__/*...*/changeRequests[0].data.CustomerName; // Bob 

那麼如何寫自己的一些輔助功能?

function extend (obj, additions) { 
    var key; 
    for (key in obj) { if (additions.hasOwnProperty(key)) { 
     obj[key] = additions[key]; 
    } 
} 

function makeChangeRequest (url, method, headers, data) { 
    var request = { 
     requestUri : url, 
     method : method, 
     headers : {}, 
     data : {} 
    }; 

    extend(request.headers, headers); 
    extend(request.data, data); 

    return request; 
} 

function getBatch (num) { return requestData.__batchRequests[num]; } 


var changeReq = makeChangeRequest("Customers", 
            "POST", 
            { "Content-ID" : "1" }, 
            { CustomerName : "Bob", CustomerID : "2012" }); 

var batch = getBatch(0); 

batch.__changeRequests.push(changeReq); 

如果你想添加更多的數據changeReq.data後:

extend(changeReq.data, { Address : "33 Nowhere Rd.", City : "Splitsville" }); 
+0

非常感謝,你的回答非常完整。 – George

1

對於你的問題的第一部分,你可以初始化data一個空關聯數組:

var requestData = { __batchRequests: [ { __changeRequests: [ 
{ requestUri: "Customers", method: "POST", headers: { "Content-ID": "1" }, data: {} } 
] } ] };  

接下來的這個部分假設,也許不正確,你可以使用jQuery。它還假定您有一個包含所有相關鍵值對的數組。

var customerDeetsArray =[{CustomerID: 400}, {CustomerName: "John"}]; 

for (var i in customerDeetsArray) { 
    requestData.data = $.extend(requestData.data, customerDeetsArray[i]); 
} 

參見工作實施例,其利用console.debug的:

http://jsfiddle.net/4Rh72/6/

+0

但** **數據裏面'.__的RequestData batchRequests [0] .__ changeRequests [0] .data'。我認爲'for'裏面的代碼應該改爲:'requestData .__ batchRequests [0] .__ changeRequests [0] .data = $ .extend(requestData .__ batchRequests [0] .__ changeRequests [0]。data,customerDeetsArray [i]);' – George

+1

謝謝你的回答和你的時間,它對我有很大的幫助。 – George

相關問題