我从 Google Directions API 收到一个字符串响应,其中包含应该表示不同语言字符的字符。类似于 ∆߬≥,类似于这些类型的字符。我认为这些字符是葡萄牙语,但无论如何我的字符串包含这样的内容:
\U00e3o
(我不确定这些是零还是'O')
我不确定这些字符的技术术语是什么,但我怎样才能在我的字符串中修复它们以便它们正确打印。
谢谢
更新:
我已经用正确的术语“unicode”更新了我的问题标题。
我检查了几个问题:
在 iPhone 上检测 NSString 中的 Unicode 字符
还有其他几个。我遵循了答案,但 unicode 字符不固定。
更新:
这是我从 GDirection 获取响应的代码。
形成请求并获得响应:
AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];
[_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
[_httpClient setDefaultHeader:@"Accept" value:@"application/json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:[NSString stringWithFormat:@"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:@"origin"];
[parameters setObject:[NSString stringWithFormat:@"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:@"destination"];
[parameters setObject:@"false" forKey:@"sensor"];
[parameters setObject:@"driving" forKey:@"mode"];
[parameters setObject:@"metric" forKey: @"units"];
NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSInteger statusCode = operation.response.statusCode;
if (statusCode == 200) {
[self parseResponse:responseObject];
} else {
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { }];
[_httpClient enqueueHTTPRequestOperation:operation];
}
从响应对象中检索信息:
- (void)parseResponse:(NSDictionary *)response {
NSString *status = [response objectForKey: @"status"];
if (![status isEqualToString: @"OK"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat: @"Google Directions Response Status: %@", status] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
}
else {
NSArray *routes = [response objectForKey:@"routes"];
NSDictionary *routePath = [routes lastObject];
if (routePath) {
NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"];
legs = [routePath valueForKey: @"legs"];
if (legs) {
/* 方向集 =============================================== ==================================================== =================================*/
directionOverview = [[NSMutableDictionary alloc] init];
NSString *legsDis = [NSString stringWithFormat: @"%@", [[legs valueForKey: @"distance"] valueForKey: @"text"]];
NSString *kmDistance = [self cutStringToPreference: legsDis];
if (kmDistance) {
[directionOverview setObject:kmDistance forKey: @"distance"];
milesLabel.text = kmDistance;
milesLabel.font = [UIFont fontWithName:@"interstate" size: 20.0];
}
NSString *durationText = [NSString stringWithFormat: @"%@", [[legs valueForKey: @"duration"] valueForKey: @"text"]];
durationText = [self cutStringToPreference: durationText];
if (durationText) {
[directionOverview setObject:durationText forKey: @"duration"];
}
NSString *startAddress = [NSString stringWithFormat: @"%@", [legs valueForKey: @"start_address"]];
startAddress = [self cutStringToPreference: startAddress];
NSString *endAddress = [NSString stringWithFormat: @"%@", [legs valueForKey: @"end_address"]];
endAddress = [self cutStringToPreference: endAddress];
[directionOverview setObject:startAddress forKey: @"origin"];
[directionOverview setObject:endAddress forKey: @"destination"];
NSArray *steps = [legs valueForKey: @"steps"];
if (steps) {
instructionArray = [[NSMutableArray alloc] init];
durationArray = [[NSMutableArray alloc] init];
distanceArray = [[NSMutableArray alloc] init];
int number = [[[steps lastObject] valueForKey: @"html_instructions"] count];
for (int i = 1; i <= number; ++i) {
NSString *instruction = [[[steps lastObject] valueForKey: @"html_instructions"] objectAtIndex: i-1];
instruction = [self cutStringToPreference: instruction];
instruction = [self flattenHTML: instruction];
instruction = [self stringByDecodingHTMLEntitiesInString: instruction];
[instructionArray addObject: instruction];
NSString *distance = [[[[steps lastObject] valueForKey: @"distance"] objectAtIndex: i-1] valueForKey: @"text"];
[distanceArray addObject: distance];
NSString *duration = [[[[steps lastObject] valueForKey: @"duration"] objectAtIndex: i-1] valueForKey: @"text"];
[durationArray addObject: duration];
}
}
}
_path = [self decodePolyLine:overviewPolyline];
NSInteger numberOfSteps = _path.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [_path objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:polyLine];
}
}
}
在标签中显示文本:
NSString *overviewAddressText = [NSString stringWithFormat: @"%@ to %@", [directionOverview objectForKey: @"origin"], [directionOverview objectForKey: @"destination"]];
overviewAddress.text = overviewAddressText;
更新:
如您所见,在标签中,文本包含这种子字符串:S/U00e3o
,这是一个包含不受支持的字符的单词。我该如何解决这个问题,以便 unicode 变成这样:São