biloshkurskyi.ss
biloshkurskyi.ss

Reputation: 1436

add NSArray to json

I have some problem with creating json what have some simple fields and fields with NSArray. I have NSMutableDictionary param what i want to convert.

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

and that's what i must get.

[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
                   )

           )

   );

But i get only -

[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"
   ),

 
For first there line i do.

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

For other lines i used similar convertation but result was failed.

Upvotes: 0

Views: 199

Answers (1)

user529758
user529758

Reputation:

You can't do this. Arrays are not dictionaries - you can't just set a key-value pair into an array. You must have overlooked something - either there should be an array in the dictionary, or there should be a dictionary instead of an array.

Upvotes: 1

Related Questions