Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从Objective-C中的NSDictionary转换UTF-8文本_Objective C_Utf 8_Nsdictionary_Nsdata_Json - Fatal编程技术网

无法从Objective-C中的NSDictionary转换UTF-8文本

无法从Objective-C中的NSDictionary转换UTF-8文本,objective-c,utf-8,nsdictionary,nsdata,json,Objective C,Utf 8,Nsdictionary,Nsdata,Json,我正在使用foursquare API获取我周围的一些位置,但当该位置的名称不是英文时,名称将如下所示: name = "\U0645\U0633\U062c\U062f \U0627\U0644\U0633\U064a\U062f\U0629 \U0639\U0627\U0626\U0634\U0629 | Aisha Mosque"; 我试图将响应转换为UTF-8,但没有改变 这是我的密码: -(void)setUpLocations{ NSURL *url = [NSURL URLWi

我正在使用
foursquare API
获取我周围的一些位置,但当该位置的名称不是英文时,名称将如下所示:

name = "\U0645\U0633\U062c\U062f \U0627\U0644\U0633\U064a\U062f\U0629 \U0639\U0627\U0626\U0634\U0629 | Aisha Mosque";
我试图将
响应
转换为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"]);
}
日志结果为:

有什么建议吗

编辑: 以下是我如何从字典中提取数据:

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];
这是结果的图像

试着用这个

[NSString stringWithUTF8String:]
没问题

NSLog()调用
NSDictionary
NSArray
description
方法,并将所有非ASCII字符打印为
\unnn
转义序列

如果从字典中提取字符串值并打印,您将看到 一切都是正确的

简单的例子:

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

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

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

更新:问题似乎出在
removewitespaces:
方法中,因为

NSString *str = [NSString stringWithFormat:@"%@",dic];
已使用
description
方法将字典转换为字符串, 下面的
stringByReplacingOccurrencesOfString
调用非常糟糕(抱歉!) 方法来解决这个问题

您应该改为使用
objectForKey
访问字典键,或枚举 使用
for(NSString*键入dic){…}
创建字典,并构建所需的 数组


更新2:从JSON数据(发布在聊天讨论中)中,您似乎只需要

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

请注意,“groups”是一个只有一个元素的数组。

不仅仅是打印它,我还将它放在了UITableView单元格中,得到了相同的结果,请注意。@Mutawe:那么您没有正确提取字符串值。也许您可以将
stringWithFormat
与字典参数一起使用来设置单元格的文本?结果是\U0645\U0633。。。。在调用removeWhiteSpaces:method之前也出现了。我尝试了您的答案,请参阅更新。@Mutawe:在调试器中检查
dic
value
的类。
dic
真的是字典还是数组?是否
value
实际上是一个数组请注意,应用于数组的
valueForKey
返回一个数组!如果我尝试使用objectForKey而不是calueForKey,我会得到一个错误:-[\uu NSArrayI objectForKey:]:无法识别的选择器发送到实例0x9939250
NSArray *itemsArray = json[@"response"][@"groups"][0][@"items]; 
NSArray *namesArray = [itemsArray valueForKey:@"name"];