0

我在创建 json 时遇到了一些问题,它有一些简单的字段和NSArray. 我有NSMutableDictionary我想要转换的参数。

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]]];

对于其他行,我使用了类似的转换,但结果失败了。

4

1 回答 1

1

你不能这样做。数组不是字典——您不能只将键值对设置到数组中。您一定忽略了一些东西——字典中应该有一个数组,或者应该有一个字典而不是数组。

于 2012-09-05T19:19:12.823 回答