Convert UTF8 strings in JSON to plain text iOS?

2019-08-29 18:33发布

问题:

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