编码的nsdata utf8 json,在ios中带有重音字符

编码的nsdata utf8 json,在ios中带有重音字符,ios,json,utf-8,nsstring,non-ascii-characters,Ios,Json,Utf 8,Nsstring,Non Ascii Characters,我向一个Web服务器发出post请求,该服务器用JSON回答我,这是响应的标题: Cache-Control: private Content-Length: 826 Content-Type: application/json; charset=utf-8 Date: Wed, 04 Feb 2015 05:53:59 GMT Server: Microsoft-IIS/8.5 Set-Cookie: ASP.NET_SessionId=0w0mile5232yoqqdlcdomwgf; pa

我向一个Web服务器发出post请求,该服务器用JSON回答我,这是响应的标题:

Cache-Control: private
Content-Length: 826
Content-Type: application/json; charset=utf-8
Date: Wed, 04 Feb 2015 05:53:59 GMT
Server: Microsoft-IIS/8.5
Set-Cookie: ASP.NET_SessionId=0w0mile5232yoqqdlcdomwgf; path=/; HttpOnly
X-Aspnet-Version: 4.0.30319
X-Powered-By: ASP.NET
我解析数据响应(NSURLConnection)并序列化json:

NSArray *arrayFromServer = [NSJSONSerialization JSONObjectWithData:dataFromServerArray options:NSJSONReadingAllowFragments error:&error];
NSLog(@"string: %@",arrayFromServer);
输出:

string: (
    {
    altitude = 0;
    battery = 100;
    "dev_last_contact" = 1423031944;
    "device_end_licence" = 1430398189;
    "device_type" = hw;
    "email_notification" = 0;
    "end_user_type" = 0;
    "fstk_cnt" = 15;
    "fstk_ts" = 0;
    heading = 0;
    id = 62;
    imei = 0123456789APPLE;
    inversegeo = "gsm_position ";
    label = "Test\Ufffd";
    latitude = "45.503731";
    "licence_level" = 2;
    "licence_status" = valid;
    "licence_type" = 1;
    longitude = "11.90365";
    "md5_image" = CA137B2CB710BC15C87BC6A54D305A2B;
    "movement_alert" = 0;
    "on_movement" = 0;
    "pos_acy" = 1977;
    "push_notification" = 0;
    refresh = 30;
    "req_pos_cnt" = 0;
    "req_pos_ts" = 0;
    rup = "user_device_list.aspx";
    "secure_area" = 0;
    "serial_nr" = IOS;
    speed = 0;
    "text_notification" = 0;
    timestamp = 1423031944;
    "url_image" = "http://XXXXX.XXXX.XXXX.XXXX/AppImages/1422966751507_62.jpg";
}
)
然后我创建objectsArray,如下所示:

devicesArray = [[NSMutableArray alloc] init];
for(NSDictionary *eachDevice in arrayFromServer)
{
    //NSLog(@"DEVICE: %@",eachDevice);

    Device *device = [[Device alloc] initWithJSONData:eachDevice];

    NSString *device_id = [NSString stringWithFormat:@"%ld",(long)[device deviceId]];
    [self.all_devices_id addObject:device_id];

    [devicesArray addObject:device];
}
我的目标很简单:

.h
@interface Device : NSObject
-(id)initWithJSONData:(NSDictionary*)data;
...

.m
-(id)initWithJSONData:(NSDictionary*)data{
self = [super init];
if(self){
    self.deviceId = [[data objectForKey:@"id"] integerValue];

    NSLog(@"data label : %@",[data objectForKey:@"label"]);

    self.label =  [data objectForKey:@"label"];
...
输出为:

data label : Test�
如您所见,“标签”在“Test\Ufffd”中转换,然后在“Test”中转换� 但应该是“测试” 我对重音字符有问题,如何正确编码这个字符串

谢谢。

试试看

[[data objectForKey:@"label"] UTF8String]; 
以前

NSString *utf = [[data objectForKey:@"label"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

您的代码没有问题,因为\Ufffd对于� 而不是为了你。表示服务器返回了错误的值。è的正确unicode是\U00e8,您可以找到它,对于� 找到。我希望这能解决您的问题。

不,很遗憾,不起作用,NSLog(@“数据标签:%s”,[[data objectForKey:@“标签”]UTF8String]);输出:数据标签:TestÔøΩNSString*utf=[[data objectForKey:@“label”]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];输出:数据标签:测试�我认为是网络服务器没有返回正确的数据。我会联系网站管理员。谢谢。好的,我会联系服务器管理员,希望他们能帮助我。谢谢你的帮助helpm@ilmetu是的,我已经通过将您的JSON存储到我的本地服务器进行了检查,并且运行良好。因此,只需联系您的服务器管理员,它就可以正常工作。服务器管理员说问题出在我的请求上,因为在db中字符是\Ufffd。。。所以它还没有解决,但我接受你的答案,别担心