2012-09-05 84 views
0

我在創建json時遇到了一些與NSArray有一些簡單字段和字段的問題。 我有NSMutableDictionary param我想要轉換。添加NSArray到json

param = { 
controller = Analytics; 
method = saveAnalytics; 
timeParameters =  { 
    data =   (
        { 
      "device_id" = 56; 
      "step_id" = 1; 
      "step_time" = 1; 
      time = 1346869802; 
      udid = "00000000-0000-1000-8000-000C292A9593"; 
      "user_id" = 1; 
     }, 
        { 
      "device_id" = 56; 
      "step_id" = 6; 
      "step_time" = 1; 
      time = 1346869802; 
      udid = "00000000-0000-1000-8000-000C292A9593"; 
      "user_id" = 1; 
     } 
); 
    token = 4217bd9d1fbdb693ef4d360a8766ec94923ad273a14440f22071b59b243f0db5; 
}; 

這就是我必須得到的。

[controller] => Analytics 
[method] => saveAnalytics 
[parameters] => Array 
    (
     [token] => 6fa97f6f5cc5e0de4e98f17de2acad5d52485be0df6f1da1a81726f6bbdf9a74 
     [data] => Array 
      (
       [0] => Array 
        (
         [device_id] => 60 
         [step_id] => 1 
         [step_time] => 1 
         [udid] => 00000000-0000-1000-8000-000C292A9593 
         [user_id] => 13 
         [time] => 1346769966 
        ) 

       [1] => Array 
        (
         [device_id] => 60 
         [step_id] => 5 
         [step_time] => 2 
         [udid] => 9b86e254 
         [user_id] => 13 
         [time] => 1346661111 
        ) 

       [2] => Array 
        (
         [device_id] => 60 
         [step_id] => 20 
         [step_time] => 3 
         [udid] => 9b86e254 
         [user_id] => 13 
         [time] => 1346661111 
        ) 

       [3] => Array 
        (
         [device_id] => 60 
         [step_id] => 20 
         [step_time] => 3 
         [udid] => 9b86e254 
         [user_id] => 13 
         [time] => 1346661111 
        ) 

      ) 

    ); 

但我只得到 -

[controller] => Analytics 
[method] => saveAnalytics 
[parameters] => (
"token=877cec7e45465ce48dd06aa58b42c502c23fe2111b21770a5d9b6092bb939ea8", 
    data = (
      0 = (
     "user_id=1", 
     "device_id=56", 
     "step_time=1", 
     "time=1346781516", 
     "step_id=1", 
     "udid=00000000-0000-1000-8000-000C292A9593" 
    ), 


對於第一行有我做的。

[paramArray addObject:[NSString stringWithFormat:@"%@=%@", key, [params objectForKey:key]]]; 

對於其他行我使用類似的轉換,但結果失敗。

回答

1

你不能這樣做。數組不是字典 - 你不能只將一個鍵值對設置成一個數組。你一定忽略了一些東西 - 或者應該在字典中有一個數組,或者應該有一個字典而不是一個數組。