将NSArray添加到json

时间:2012-09-05 19:10:58

标签: json nsmutablearray request nsarray

我在创建json时有一些问题,那里有一些带有NSArray的简单字段和字段。 我有NSMutableDictionary param我要转换的内容。

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 个答案:

答案 0 :(得分:1)

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