Question

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.

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top