i make a post request to a webserver that answer me with a JSON, this is the header of the response :
我向一个用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
i parse the data response (NSURLConnection) and serialize the json :
我解析数据响应(NSURLConnection)并序列化json:
NSArray *arrayFromServer = [NSJSONSerialization JSONObjectWithData:dataFromServerArray options:NSJSONReadingAllowFragments error:&error];
NSLog(@"string: %@",arrayFromServer);
output :
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";
}
)
then i create the objectsArray like this:
然后我像这样创建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];
}
my object is very simple :
我的目标非常简单:
.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"];
...
the output is :
输出是:
data label : Test�
as you can see the "label" is converted in "Test\Ufffd" and then Test� but should be "Testè" i have problem with the accented char, how can i encode this string correctly?
你可以看到“标签”转换为“Test \ Ufffd”然后Test 但应该是“Testè”我有重音字符的问题,我怎么能正确编码这个字符串?
Thanks.
2 个解决方案
#1
5
There is no problem with your code because \Ufffd is unicode for � and not for è. Means your server is returning you the wrong value. the correct unicode for è is \U00e8 you can find it here, and for � find here. I hope this will solve your problem.
您的代码没有问题,因为\ Ufffd是un的unicode而不是è。意味着您的服务器返回错误的值。 è的正确unicode是\ U00e8你可以在这里找到它,并且 在这里找到。我希望这能解决你的问题。
#2
2
Try
[[data objectForKey:@"label"] UTF8String];
before
NSString *utf = [[data objectForKey:@"label"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
#1
5
There is no problem with your code because \Ufffd is unicode for � and not for è. Means your server is returning you the wrong value. the correct unicode for è is \U00e8 you can find it here, and for � find here. I hope this will solve your problem.
您的代码没有问题,因为\ Ufffd是un的unicode而不是è。意味着您的服务器返回错误的值。 è的正确unicode是\ U00e8你可以在这里找到它,并且 在这里找到。我希望这能解决你的问题。
#2
2
Try
[[data objectForKey:@"label"] UTF8String];
before
NSString *utf = [[data objectForKey:@"label"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];