gpt4 book ai didi

ios - 将 JSON 中的 UTF8 字符串转换为纯文本 iOS?

转载 作者:行者123 更新时间:2023-11-29 01:37:49 25 4
gpt4 key购买 nike

我正在尝试在 UITableViewCell 中显示 JSON 列表。当我解析我的 JSON 时,我可以看到 UTF8 字符,当我显示 UILabel 时,UTF8 字符串显示错误的值。

这是我的JSON文件结构

 [{"name":"C\u0103r\u021bile pe fa\u021b\u0103 (Taxi Gratis)","code":"TX7","details":"tertert","costperkm":"ertrete","minimumcost":"tetret","taxicontact":[{"carrier":"ertertert","number":"ert"}],"taxistation":[{"stationname":"terterter","latitude":"tertert","longitude":"rete","details":"terterterter"}],"logo":"","tag":"tertertertert"}]

我正在尝试在我的 UILabel 中显示此 "name":"C\u0103r\u021bile pe fa\u021b\u0103 (Taxi Gratis)" 并且其显示如下 "C? r?ile> pe fa??"

我试过的代码

 NSString *correctString = [NSString stringWithCString:[ss cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSUTF8StringEncoding];

NSLog(@"%@",correctString);

请帮我解决这个问题。

最佳答案

我试过这段代码,它运行正确:

NSArray *jsonArray = @[
@{
@"name":@"C\u0103r\u021bile pe fa\u021b\u0103 (Taxi Gratis)",
@"code":@"TX7",
@"details":@"tertert",
@"costperkm":@"ertrete",
@"minimumcost":@"tetret",
@"taxicontact":@[
@{
@"carrier":@"ertertert",
@"number":@"ert"
}
],
@"taxistation":@[
@{
@"stationname":@"terterter",
@"latitude":@"tertert",
@"longitude":@"rete",
@"details":@"terterterter"
}
],
@"logo":@"",
@"tag":@"tertertertert"
}
];

NSDictionary *jsonDict = [jsonArray objectAtIndex:0];

NSString *str = [jsonDict objectForKey:@"name"];
NSString *correctString = [NSString stringWithCString:[str cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSUTF8StringEncoding];

NSLog(@"%@",correctString);
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 280, 40)];
lbl.backgroundColor = [UIColor cyanColor];
lbl.text = correctString;
[self.view addSubview:lbl];

enter image description here

关于ios - 将 JSON 中的 UTF8 字符串转换为纯文本 iOS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32772037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com