I am new in JSON parsing, I am using following code to post data to server
NSDictionary *newDatasetInfo = [NSDictionary dictionaryWithObjectsAndKeys:ClassicLevel, @"ClassicLevel",CurrentLevel,@"CurrentLevel",UpdatedDate,@"UpdatedDate",Name,@"Name",UpdatedTime,@"UpdatedTime", nil];
NSError*error;
//convert object to data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:kNilOptions error:&error];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://testURL"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsonData];
By using this I am able to generate following request body
{
"Name":"abc",
"UpdatedDate":"",
"CurrentLevel":"2",
"UpdatedTime":"122",
"ClassicLevel":"1"
}
But I have to generate request body like:
{
"objTimesheet" :
{
"Name":"abc",
"UpdatedDate":"",
"CurrentLevel":"2",
"UpdatedTime":"122",
"ClassicLevel":"1"
}
}
Thanks in Advance.
You are looking for a dictionary inside a dictionary. Going off what you already have:
NSDictionary *newDatasetInfo = [NSDictionary dictionaryWithObjectsAndKeys:ClassicLevel, @"ClassicLevel",CurrentLevel,@"CurrentLevel",UpdatedDate,@"UpdatedDate",Name,@"Name",UpdatedTime,@"UpdatedTime", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:newDatasetInfo, @"objTimesheet", nil];
NSError*error;
//convert object to data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.