【问题标题】:Convert UTF8 strings in JSON to plain text iOS?将 JSON 中的 UTF8 字符串转换为纯文本 iOS?
【发布时间】:2015-12-22 16:29:57
【问题描述】:

我正在尝试在 UITableViewCell 中显示 JSON 列表。 当我解析我的 JSON 时,我可以看到 UTF8 字符,当我显示 UILabels 时,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);

请帮我解决这个问题。

【问题讨论】:

  • 什么是“纯文本”?你希望字符串如何显示?

标签: ios objective-c json utf-8


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多