传递带有希伯来值问题的JSON数据(UTF8?)

时间:2012-05-07 18:13:25

标签: objective-c json utf-8 sbjson

我有这个方法 -

NSArray *objects = [NSArray arrayWithObjects:appDelegate.UserID,itemName.text,@"1",@"1",itemDegem.text,itemDescription.text, nil];
NSArray *keys = [NSArray arrayWithObjects:@"userId",@"itemName",@"itemCategoryId",@"regionId",@"degemName",@"itemDescription", nil];

NSDictionary *registerDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *jsonRequest = [registerDict JSONRepresentation];    
NSLog(@"jsonRequest is %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"http://www.somedomain.com/method"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
    self.responseData = [NSMutableData data];
}

但是当我在我的对象中发送字符串而不是英文时我从服务器收到错误。 现在,我知道它的UTF8问题,但我确实把它设置为UTF8。有人可以帮我解决一下吗?

1 个答案:

答案 0 :(得分:4)

我认为错误strlen([jsonRequest UTF8String])不等于[jsonRequest length]

使用NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];使用UTF8编码将NSString转换为NSData。