关于objective c:传递带有希伯来值问题的JSON数据(UTF8?)

passing JSON data with hebrew values problems (UTF8?)

我有这个方法-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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。有人能帮我弄清楚吗?


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

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