简体   繁体   中英

Objective-C reading file content returns incorrect encoded text with NSUTF8StringEncoding

I have an app that has a script which reads file for json content. Script is:

    NSString *jsonString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", jsonString);
    //filepath debug log: /var/mobile/Applications/1394B0D1-5B12-445D-BDFA-7B1E2661B685/Documents/json_branch_1_ru



In iPhone Simulator everything works perfect, returning text is correct:

{"id":1,"add":"Казахстан, Алматы, улица Толе би, 73а","lon":"43.254677","lat":"76.934434","bnkid":42,"al":"","rat":10,"prod":"","valid":1,"wrktime":"с физ. лицами: понедельник-пятница: 09:00-18:00 с юр. лицами: Нет данных"}

When I test app on my device , returning text encoding is incorrect:

{"id":1,"add":"–ö–∞–∑–∞—Ö—Å—Ç–∞–Ω, –ê–ª–º–∞—Ç—ã, —É–ª–∏—Ü–∞ –¢–æ–ª–µ –±–∏, 73–∞","lon":"43.254677","lat":"76.934434","bnkid":42,"al":"","rat":10,"prod":"","valid":1,"wrktime":"—Å —Ñ–∏–∑. –ª–∏—Ü–∞–º–∏: –ø–æ–Ω–µ–¥–µ–ª—å–Ω–∏–∫-–ø—è—Ç–Ω–∏—Ü–∞: 09:00-18:00 —Å —é—Ä. –ª–∏—Ü–∞–º–∏: –ù–µ—Ç –¥–∞–Ω–Ω—ã—Ö"},{"id":2,"add":"–ö–∞–∑–∞—Ö—Å—Ç–∞–Ω, –ê–ª–º–∞—Ç—ã, –º–∫—Ä. 5, 30","lon":"43.231442","lat":"76.867923","bnkid":42,"al":"","rat":10,"prod":"","valid":1,"wrktime":"09:00 - 18:00"}


What is the problem?
Note:
1. My file doesn't have extension
2. File saved with utf-8 encoding

I solved this problem.
The problem was Line Breaks or tabs in json text. Somehow it damaged text encoding.

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