我正在使用包含对象数组的 Web 服务。但是在服务器端,他们只获得所有字段的空值。
客户端代码是:
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:urlRequest
delegate:self];
//************DATA formation
NSMutableDictionary *jsonDict = [[NSMutableDictionary alloc]init];
NSMutableDictionary *jsonDict1 = [[NSMutableDictionary alloc]init];
[jsonDict setObject:@"3" forKey:@"rollNo"];
[jsonDict setObject:@"Ezhil" forKey:@"FirstName"];
[jsonDict setObject:@"Arasu" forKey:@"LastName"];
[jsonDict1 setObject:@"4" forKey:@"rollNo"];
[jsonDict1 setObject:@"XYZ" forKey:@"FirstName"];
[jsonDict1 setObject:@"ABC" forKey:@"LastName"];
NSArray *jsonArray=[[NSArray alloc]initWithObjects:jsonDict,jsonDict1, nil];
//Converting to JSON string.
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSString *jsonString = [writer stringWithObject:jsonArray];
NSLog(@"JSON String : %@",jsonString);
//************Setting DATA in URL
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[connection start];
// Code for response....
我不知道我错了。任何人都可以帮助我解决这个问题。提前致谢。