JSON数组格式

时间:2011-08-14 06:49:35

标签: iphone objective-c arrays json

我正在开发一个iPhone应用程序,我必须以JSON数组格式发布我的数据,我尝试并发送它,但它没有在后端获得JSON数组。我使用以下代码创建和发送我的数组:

NSMutableArray *playsArr=[[NSMutableArray alloc]init];

int score=[[NSNumber numberWithInt:100 ]intValue];
int game_id=[[NSNumber numberWithInt:1 ]intValue];

NSString *game_version=@"1.1";

// Time stamp in Unix format
int timestamp=[[NSNumber numberWithInt:1312582440]intValue];
int elapsed_time=[[NSNumber numberWithInt:500]intValue];

int level_reached=[[NSNumber numberWithInt:1 ]intValue];

NSString *platform=@"ios";
NSString *hash=@"801356a3b23c8e2ed94455545ddere";

NSString *verificationNew=@"676c66df9b529895a97cd5f4d55ef";

NSLog(@"%@",verification);


NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

[dict setObject:[NSNumber numberWithInt:score] forKey:@"score"]; 
[dict setObject:[NSNumber numberWithInt:game_id] forKey:@"game_id"];
[dict setObject:game_version forKey:@"game_version"];
[dict setObject:platform forKey:@"platform"];


[dict setObject:[NSNumber numberWithInt:timestamp] forKey:@"timestamp"]; 
[dict setObject:[NSNumber numberWithInt:elapsed_time] forKey:@"elapsed_time"];
[dict setObject:hash forKey:@"hash"];
[dict setObject:[NSNumber numberWithInt:level_reached] forKey:@"level_reached"];
[dict setObject:verification forKey:@"verification"];

NSLog(@"%@",dict);


[playsArr addObject:dict];

[request addPostValue:playsArr forKey:@"plays"];


[request setTimeOutSeconds:600];

[request setDelegate:self];
[request setDidFailSelector:@selector(uploadFailed:)];
[request setDidFinishSelector:@selector(uploadFinished:)];
[request startAsynchronous];

现在我从后端获得的评论是,他们没有得到的是数组及其返回错误或空响应。

我在形成JSON数组格式时犯了错误? 请帮我编写代码行......

提前致谢。

我必须做出这样的结构......

阵 (   [plays] =>排列       (           [0] => [得分] = 513956           [1] => [game_id] = 1           [2] => [game_version] = 1.0           [3] => [平台] =网络           [4] => [timestamp] = 1313146039           [5] => [elapsed_time] = 400           [6] => [hash] = 61e51000143566bfd2e3aefa7cb7b2ad           [7] => [level_reached] = 5           [8] => [验证] = e56a35341c8dd44fa082ad678cb11593       )   [module] =>玩   [action] =>保存 )

请帮忙......

1 个答案:

答案 0 :(得分:1)

您的代码根本不使用JSON。根本没有代码可以将您的数据转换为JSON表示。无论是iOS还是 ASIHTTPRequest (你可能在没有提及的情况下使用)都内置了对JSON的支持。

因此,您需要做的是将JSON库(例如TouchJSON)添加到项目中,并将dict字典转换为JSON表示。然后将其作为请求主体添加到ASIHTTPRequest(而不是POST值):

NSError *error = NULL;
NSData *jsonData = [[CJSONSerializer serializer] serializeObject:dict error:&error];
[request addRequestHeader: @"Content-Type" value: @"application/json"];
[request appendPostData:data];

<强>更新

根据您的更新,我假设您要创建以下JSON数据(您的符号对我来说并不是很清楚,因为它不区分数组和字典,因为它似乎同时具有数字和命名字典元素的关键字):

{
  "plays": [
    {
      "score": 513956,
      "game_id": 1,
      "game_version": "1.0",
      "platform": "Web",
      "timestamp": 1313146039,
      "elapsed_time": 400,
      "hash": "61e51000143566bfd2e3aefa7cb7b2ad",
      "level_reached": 5,
      "verification": "e56a35341c8dd44fa082ad678cb11593"
    }
  ],
  "module": "play",
  "action": "save"
}

您可以使用以下代码对其进行对照:

NSMutableDictionary *dict= [[NSMutableDictionary alloc] init];

NSMutableArray *plays = [[NSMutableArray alloc] initWithCapacity:8];

NSMutableDictionary *play= [[NSMutableDictionary alloc] init];
[play setObject:[NSNumber numberWithInt:score] forKey:@"score"]; 
[play setObject:[NSNumber numberWithInt:game_id] forKey:@"game_id"];
[play setObject:game_version forKey:@"game_version"];
[play setObject:platform forKey:@"platform"];
[play setObject:[NSNumber numberWithInt:timestamp] forKey:@"timestamp"]; 
[play setObject:[NSNumber numberWithInt:elapsed_time] forKey:@"elapsed_time"];
[play setObject:hash forKey:@"hash"];
[play setObject:[NSNumber numberWithInt:level_reached] forKey:@"level_reached"];
[play setObject:verification forKey:@"verification"];
[plays addObject:play];
[play release];

[dict setObject:plays forKey:@"plays"];
[plays release];

[dict setObject:@"play" forKey:@"module"];
[dict setObject:@"save" forKey:@"action"];

NSError *error = NULL;
NSData *jsonData = [[CJSONSerializer serializer] serializeObject:dict error:&error];
[request addRequestHeader: @"Content-Type" value: @"application/json"];
[request appendPostData:data];