简体   繁体   中英

iOS : decode utf8 string

I'm receiving a json data from server with some strings inside. I use SBJson https://github.com/stig/json-framework to get them.

However when I output some strings at UILabel they look like this: \И\л\ь\я\Б\л\о\х (that's Cyrillic symbols)

And it's all right with latin characters

How can I decode it into normal symbols?

Some code about getting data:

   NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];   
   NSString *stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
   NSDictionary *object = [parser objectWithString:stringData error:nil];
   NSString *comments = [NSString stringWithFormat:@"%@",[object valueForKey:@"comments"]];

String comments has a very special format, so I'm doing some operation like stringByTrimmingCharactersInSet ,

stringByReplacingOccurrencesOfString ,

NSArray* json_fields = [comments_modified componentsSeparatedByString: @";"];

to get a final data.

This is an example of received data after some trimming/replacing (it's NSString* comments ):

"already_wow"=0;"date_created"="2012/03/1411:11:18";id=41598;name="\U0418\U043b\U044c\U044f\U0411\U043b\U043e\U0445";text="\U0438\U043d\U0442\U0435\U0440\U0435\U0441\U043d\U043e";"user_id"=1107;"user_image"="user_image/a6/6f/96/21/20111220234109510840_1107.jpg";"user_is_deleted"=0;username=IlyaBlokh;"wow_count"=0;

You see that fields text and name are encoded

If I display them on the view (at UILabel for example), they still look the same

可能返回的字符串只是unicode字符串表示形式(ascii字符串),这意味着未返回用utf8编码的内容,请尝试使用NSASCIIStringEncoding来获取stringData

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM