2012-06-08 50 views
1

這是我從我的服務接收到的JSON 它將包含「第一個」 - Array和ResponseStatus數組。Knockout.js映射JSON對象 - 只有第一個數組

{"Customers":[{"Id":1,"Name":"Thomas","LastName":"Deutsch"}, {"Id":2,"Name":"Julia","LastName":"Baumeistör"}],"ResponseStatus":{}}   

這第一JSON數組映射與ko.mapping

$.getJSON(self.Url, function (data) { 
    ko.mapping.fromJS(data.Customers, {}, self.Items); 
}); 

它工作得很好。但這裏是

問題:我不能使用「data.Customers」,因爲服務器也可能會返回一個「公司」的JSON數組的第一個位置。

問題: 是一個映射可能的,即只解析「FIRST」陣?我堅持....

+0

你是什麼意思,當你說「的第一個位置JSON陣列‘’服務器也可以返回一個」公司? –

回答

0

這可能爲你工作正在試圖做什麼

$.getJSON(self.Url, function (data) { 
    $.each(data, function(i, item){ 

     var customers = getPersonnel(item.Customers); 
     var companies = getPersonnel(item.Companies); 
     var response = getPersonnel(item.ResponseStatus); 

     if(customers != null){ 
      // Parse JSON here... 
     } 

     if(companies != null){ 
      // Parse JSON here... 
     } 

    }); 

});