Convert UTF8 strings in JSON to plain text iOS?

非 Y 不嫁゛ 提交于 2020-01-07 06:55:11

问题


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.

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 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??"

Code i have tried

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

    NSLog(@"%@",correctString);

Please help me to get solution for this.


回答1:


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



来源:https://stackoverflow.com/questions/32772037/convert-utf8-strings-in-json-to-plain-text-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!