如何在Ios中为json解析在请求体中设置对象名

如何在Ios中为json解析在请求体中设置对象名,ios,objective-c,nsdictionary,nsjsonserialization,Ios,Objective C,Nsdictionary,Nsjsonserialization,我是JSON解析方面的新手,我使用以下代码将数据发布到服务器 NSDictionary *newDatasetInfo = [NSDictionary dictionaryWithObjectsAndKeys:ClassicLevel, @"ClassicLevel",CurrentLevel,@"CurrentLevel",UpdatedDate,@"UpdatedDate",Name,@"Name",UpdatedTime,@"UpdatedTime", nil]; NSE

我是JSON解析方面的新手,我使用以下代码将数据发布到服务器

    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];
通过使用它,我能够生成以下请求体

{
 "Name":"abc",
 "UpdatedDate":"",
 "CurrentLevel":"2",
 "UpdatedTime":"122",
 "ClassicLevel":"1" 
}
但我必须生成请求主体,如:

{
"objTimesheet" : 
    {
    "Name":"abc",
    "UpdatedDate":"",
    "CurrentLevel":"2",
    "UpdatedTime":"122",
    "ClassicLevel":"1" 
    }
}

提前谢谢。

您正在词典中查找词典。离开你已经拥有的:

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];

你正在字典里找一本字典。离开你已经拥有的:

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];

您好,我必须传递相同的请求,但在请求中传递用户名和密码的值有什么想法吗?@keyurbhalodiya:试试这个..NSDictionary*newDatasetInfo=[NSDictionary dictionaryWithObjectsAndKeys:strUsername、@usernameKey、strPassword、@passwordKey、nil];。。希望这对你有所帮助。你好,我必须传递相同的请求,但在请求中传递用户名和密码的值有什么想法吗?@keyurbhalodiya:试试这个..NSDictionary*newDatasetInfo=[NSDictionary dictionaryWithObjectsAndKeys:strUsername、@usernameKey、strPassword、@passwordKey、nil];。。希望这对你有帮助。