Im using foursquare API
to get some locations around me, but when the name of that place wasn't in english, the name will be like follows:
name = "\U0645\U0633\U062c\U062f \U0627\U0644\U0633\U064a\U062f\U0629 \U0639\U0627\U0626\U0634\U0629 | Aisha Mosque";
i tried to convert the response
to a UTF-8 but nothing changed.
Here is my code:
-(void)setUpLocations{
NSURL *url = [NSURL URLWithString: @"https://api.foursquare...."];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"Response: %@",[[[json objectForKey:@"response"]objectForKey:@"groups"]valueForKey:@"items"]);
}
And the log result is:
contact = { }; id = 51712507498ec4e8c5ae9f48; likes = { count = 0; groups = ( ); }; location = { address = Abdoun; cc = JO; city = Amman; country = Jordan; distance = 3819; lat = "31.95406043797281"; lng = "35.88095228186612"; }; name = "\\U0645\\U0633\\U062c\\U062f \\U0627\\U0644\\U0633\\U064a\\U062f\\U0629 \\U0639\\U0627\\U0626\\U0634\\U0629 | Aisha Mosque"; restricted = 1; stats = { checkinsCount = 43; tipCount = 2; usersCount = 23; }; verified = 0; },
Any Suggestions ??
EDIT: here is how i extract the data from the dictionary:
NSDictionary *dic = [[[[json objectForKey:@"response"]objectForKey:@"groups"]valueForKey:@"items"] copy];
namesArray = [[NSArray alloc]initWithArray:[self removeWhiteSpaces:[dic valueForKey:@"name"]]];
-(NSArray *)removeWhiteSpaces:(NSDictionary *)dic{
NSString *str = [NSString stringWithFormat:@"%@",dic];
NSString *str2 = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *secondString = [str2 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *thirdString = [secondString stringByReplacingOccurrencesOfString:@"(" withString:@""];
NSString *forthString = [thirdString stringByReplacingOccurrencesOfString:@")" withString:@""];
NSString *fifthString = [forthString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSArray *items = [fifthString componentsSeparatedByString:@","];
return items;
}
And in the UITableView
:
cell.textLabel.text = [NSString stringWithFormat:@"Name: %@ ",[namesArray objectAtIndex:indexPath.row] ];
Update
After trying @Martin R answer i got the same results:
NSDictionary *dic = [[[[json objectForKey:@"response"]objectForKey:@"groups"]valueForKey:@"items"] copy];
NSString *value =[dic valueForKey:@"name"];
NSLog(@"%@", value);
UILabel *lbl = [[UILabel alloc]initWithFrame:self.view.frame];
lbl.numberOfLines = 0;
lbl.text = [NSString stringWithFormat:@"%@",value];;
[self.view addSubview:lbl];
and here is an image of the result
There is no problem.
NSLog() calls the description
method of NSDictionary
and NSArray
, and that prints all non-ASCII characters as \\Unnnn
escape sequence.
If you extract the string values from the dictionary and print that you will see that everything is correct.
Simple example:
NSDictionary *dict = @{ @"currency": @"€" };
NSLog(@"%@", dict);
// Output: { currency = "\U20ac"; }
NSString *value = dict[@"currency"];
NSLog(@"%@", value);
// Output: €
UPDATE: The problem seems to be in your removeWhiteSpaces:
method, because
NSString *str = [NSString stringWithFormat:@"%@",dic];
already uses the description
method to convert the dictionary to a string, and the following stringByReplacingOccurrencesOfString
calls are a (sorry!) very bad method to fix that.
You should access the dictionary keys with objectForKey
instead, or enumerate the dictionary with for (NSString *key in dic) { ... }
and build the desired array.
UPDATE 2: From the JSON data (posted in chat discussion) it seem that you just need
NSArray *itemsArray = json[@"response"][@"groups"][0][@"items];
NSArray *namesArray = [itemsArray valueForKey:@"name"];
Note that "groups" is an array with one element.
尝试使用此。
[NSString stringWithUTF8String:]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.