将数据发布为JSON格式

时间:2011-08-16 06:10:56

标签: objective-c arrays cocoa-touch json

我正在开发iPhone应用程序。在这里,我尝试使用以下代码发布我的数据:

    NSDictionary *contactData = [NSDictionary dictionaryWithObjectsAndKeys:  
                             [NSNumber numberWithInt:score] ,@"score",  
                             [NSNumber numberWithInt:game_id] ,@"game_id",  
                             game_version ,@"game_version",  
                             platform ,@"platform",  
                             [NSNumber numberWithInt:timestamp] ,@"timestamp",  
                             [NSNumber numberWithInt:elapsed_time] ,@"elapsed_time",
                             hash ,@"hash",
                             [NSNumber numberWithInt:level_reached] ,@"level_reached",
                             verification ,@"verification",
                             nil];
[request1 addPostValue:contactData forKey:@"plays[]"];

我想生成以下格式:

Array
(
   [plays] => Array
       (
           [0] => Array
                       (
                        [score] = 513956
                       [game_id] = 1
                         [game_version] = 1.0
                 [platform] = Web
                   [timestamp] = 1313146039
                       [elapsed_time] = 400
                       [hash] = 61e51000143566bfddjfhdeur88cb7b2ad
                 [level_reached] = 5
                   [verification] = e56a35341c7854dref82ad678cb11593
                   )
   )
   [module] => play
   [action] => save
)

如何在必须进行更改或我犯错误的地方实施。 请帮我。我被困在那。

1 个答案:

答案 0 :(得分:0)

假设您从(?)网络服务返回有效 JSON,您可以使用JSONKit转换为JSON。

为此目的,PHP有json_encodejson_decode。请参阅here