2013-12-12 19 views
2
設置對象名稱

我在JSON解析新的,我使用下面的代碼將數據發佈到服務器如何請求主體的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" 
    } 
} 

感謝。

+0

你好,我已經通過同樣的請求,但在要求 任何想法,通過用戶名和密碼的價值? – kb920

+0

@keyurbhalodiya:試試這個..NSDictionary * newDatasetInfo = [NSDictionary dictionaryWithObjectsAndKeys:strUsername,@「usernameKey」,strPassword,@「passwordKey」,nil]; ..希望這對你有所幫助。 –

回答

4

您正在尋找字典中的字典。去關你已經擁有:

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