Reputation: 41
I am working on an iPhone app on which I have to POST my data in the JSON array format, I tried it and send it but its not getting as JSON array at back end. I used following code for creating and sending my array :
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];
Now the Comment I am getting from back end is, They are not getting is as a array and its returning error or null response.
Where I am making mistake in forming JSON array format? Please help me with code lines......
Thanks in advance.
I have to make such structure......
Array ( [plays] => Array ( [0] => [score] = 513956 [1] => [game_id] = 1 [2] => [game_version] = 1.0 [3] => [platform] = Web [4] => [timestamp] = 1313146039 [5] => [elapsed_time] = 400 [6] => [hash] = 61e51000143566bfd2e3aefa7cb7b2ad [7] => [level_reached] = 5 [8] => [verification] = e56a35341c8dd44fa082ad678cb11593 ) [module] => play [action] => save )
please help...
Upvotes: 0
Views: 2052
Reputation: 78865
Your code doesn't use JSON at all. There is simply no code to convert your data into a JSON representation. Neither iOS nor ASIHTTPRequest (which you probably use without mentioning) have built-in support for JSON.
So what you need to do is to add a JSON library (such as TouchJSON) to your project and convert the dict
dictionary into a JSON representation. Then you add it to the ASIHTTPRequest as the request body (not as a POST value):
NSError *error = NULL;
NSData *jsonData = [[CJSONSerializer serializer] serializeObject:dict error:&error];
[request addRequestHeader: @"Content-Type" value: @"application/json"];
[request appendPostData:data];
Update:
Based on your update, I assume you want to create the following JSON data (your notation isn't really clear to me as it doesn't distinguish between arrays and dictionaries and as it seems to have both a numeric and a named key for dictionary elements):
{
"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"
}
You can gerate it with the following code:
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];
Upvotes: 1