2014-04-12 120 views
1

我有這些陣列如何元素添加到陣列

int[] ivrArray = { 1, 0, 0, 0}; 
int[] agentsArray = { 0, 2, 0, 0 }; 
int[] abandonedArray = { 0, 0, 3, 0}; 
int[] canceledArray = { 0, 0, 0, 4}; 

和我用這個詞典:

Dictionary<string, int[][]> dictionary = new Dictionary<string, int[][]> 
      { 
       {"IVR", ivrArray.Select(_ => new[] {_}).ToArray()}, 
       {"Agents", agentsArray.Select(_ => new[] {_}).ToArray()}, 
       {"Abandoned", abandonedArray.Select(_ => new[] {_}).ToArray()}, 
       { "Cancelled",canceledArray.Select(_ => new[] {_}).ToArray()} 
      }; 

結果改變它之後JSON是:

{ 
    "IVR": [ 
    [1], 
    [0], 
    [0], 
    [0] 
    ], 
    "Agents": [ 
    [0], 
    [2], 
    [0], 
    [0] 
    ], 
    "Abandoned": [ 
    [0], 
    [0], 
    [3], 
    [0] 
    ], 
    "Cancelled": [ 
    [0], 
    [0], 
    [0], 
    [4] 
    ] 
} 

你可以看到JSON中的每個元素都是這樣的數組[[0],[0],[0],[4]]

我需要另一個值添加到每個元素數組中,所以結果會是這樣:

[[1325376000000,0],[1328054400000,0],[1330560000000,0],[1333238400000,0]] 

我怎麼能這樣做?

這些值對於所有四個陣列都是靜態的。

+1

沒有你在2小時前問過同樣的問題嗎? http://stackoverflow.com/questions/23028526/c-sharp-how-to-change-array-to-array-or-arrays –

+0

除此之外,它*真的*感覺就像你應該創建一個新類型與IVR屬性,代理,被遺棄等。那麼你可以只有*一個*集合。 –

+0

只需在創建數組時添加其他值:new [] {otherValue,_} – sjkm

回答

1

只是當你創建數組添加另一個值:new[] {otherValue, _}

1

做這樣的:

dictionary.Add("new values", new[] 
          { 
           new[] {123, 0}, 
           new[] {123, 0}, 
           new[] {123, 0}, 
           new[] {123, 0} 
           }); 
+0

所以我需要爲'ivr','agents','abandont'和'cancel'這四個元素做這件事,是不是有一個通用的如何做到這一點?就像我在問題中給出的那樣 –

1

JSON序列化將序列List<T>JSON陣列,從而使用列表將是一個數組優於陣列。

你的字典應該是Dictionary<string, List<List<int>>>,你就可以得到一個子列表和添加項目:

dict["some"][0].Add(0);