将JSON中的UTF8字符串转换为纯文本iOS? [英] Convert UTF8 strings in JSON to plain text iOS?

查看:114
本文介绍了将JSON中的UTF8字符串转换为纯文本iOS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Iam trying to display JSON list in UITableViewCell. When i parse my JSON i can see UTF8 characters and when i displaying UILabels the UTF8 string is displaying wrong value.

这是我的JSON文件结构

This is my JSON file structure

 [{"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"}]

am试图在我的UILabel中显示此"name":"C\u0103r\u021bile pe fa\u021b\u0103 (Taxi Gratis)",其显示方式如下所示:"C?r?ile> pe fa??"

am trying to display this "name":"C\u0103r\u021bile pe fa\u021b\u0103 (Taxi Gratis)" in my UILabel and its showing like this "C?r?ile> pe fa??"

我尝试过的代码

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

    NSLog(@"%@",correctString);

请帮助我解决此问题.

推荐答案

我尝试了此代码,并且运行正确:

I tried this code and it run correct:

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

这篇关于将JSON中的UTF8字符串转换为纯文本iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆