Objective-c解析api响应特殊字符

时间:2014-09-11 08:55:57

标签: objective-c encoding special-characters nsdata nsjsonserialization

我向API发出请求,这是接收返回的NSData:

{"word":"Ma\u00f1ana reposado"}

我试图用它来显示带有这些数据的标签,但是当我用它解析时:

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:1 error:&error];

NSString *里面[json ObjectForKey:@" word"] =" Ma?ana reposado"

如何解析NSData的正确方法?

1 个答案:

答案 0 :(得分:0)

如果数据未以空值终止,请使用initWithData:encoding

  NSString* yourNSString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

如果数据以空值终止,请使用stringWithUTF8String:以避免在末尾添加额外的\ 0(NUL终结符)

  NSString* yourNSString = [NSString stringWithUTF8String:[responseData bytes]];