簡體   English   中英

無法在Objective-C中從NSDictionary轉換UTF-8文本

[英]Unable to convert UTF-8 text from NSDictionary in Objective-C

我使用foursquare API獲取我周圍的一些位置,但是當該位置的名稱不是英語時,名稱將如下所示:

name = "\U0645\U0633\U062c\U062f \U0627\U0644\U0633\U064a\U062f\U0629 \U0639\U0627\U0626\U0634\U0629 | Aisha Mosque";

我試圖將response轉換為UTF-8,但沒有任何改變。

這是我的代碼:

-(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"]);
}

日志結果是:

  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; }, 

有什么建議么 ??

編輯:這是我從字典中提取數據的方式:

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;



}

並在UITableView

cell.textLabel.text = [NSString stringWithFormat:@"Name: %@ ",[namesArray objectAtIndex:indexPath.row] ];

更新

在嘗試@Martin R答案后,我得到了相同的結果:

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];

這是結果的圖像 UILabel與結果

沒有問題。

NSLog()調用NSDictionaryNSArraydescription方法,並將所有非ASCII字符打印為\\Unnnn轉義序列。

如果從字典中提取字符串值並打印,您將看到一切正確。

簡單的例子:

NSDictionary *dict = @{ @"currency": @"€" };

NSLog(@"%@", dict);
// Output: { currency = "\U20ac"; }

NSString *value = dict[@"currency"];
NSLog(@"%@", value);
// Output: €

更新:問題似乎出在您的removeWhiteSpaces:方法中,因為

NSString *str = [NSString stringWithFormat:@"%@",dic];

已經使用description方法將字典轉換為字符串,並且以下stringByReplacingOccurrencesOfString調用是解決此問題的一種方法(不好意思!)。

您應該改為使用objectForKey訪問字典鍵,或者使用for (NSString *key in dic) { ... }枚舉字典,然后構建所需的數組。


更新2:從JSON數據(發布在聊天討論中)看來,您只需要

NSArray *itemsArray = json[@"response"][@"groups"][0][@"items]; 
NSArray *namesArray = [itemsArray valueForKey:@"name"];

請注意,“組”是具有一個元素的數組

嘗試使用此。

[NSString stringWithUTF8String:]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM