我正在编写一个应用程序,通过http服务器界面与DirecTV接收器进行交互。我能够查询设备并获得SJON格式的响应。
以下是我的示例代码:
- (NSDictionary *) readDVRData
{
NSMutableData *rData = [[NSMutableData alloc] initWithContentsOfFile:@"./data"];
//NSLog(@"rData = %@",rData);
NSString *rString = [[NSString alloc] initWithData:rData encoding:NSUTF8StringEncoding];
NSLog(@"rString = %@",rString);
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSDictionary *response = [json objectWithString:rString error:&error];
//NSLog(@"response = %@",response);
return response;
}
出于测试目的,我将响应保存到数据文件中,并且我正在上面的示例中读取它。通过以下方式从NSMutableData转换为NSString:
NSString *rString = [[NSString alloc] initWithData:rData encoding:NSUTF8StringEncoding];
如果有以下字符,将失败。 È,如CrÈmeFraiche。作为参考,这是在南方公园录音的情节标题和描述中。
如果我将此字符更改为大写“E”,则转换有效。是否有一种不同的方法我应该用来捕捉这样的东西?或者有一种简单的方法来删除这样的字符?请记住,如果它的重点标记或可能导致转换失败的不同标记,我将不会事先知道。
谢谢, 戴夫
如果需要,可以使用数据文件片段。我会包含整个文本,但它会将字符限制推高到30000以上。
{
"groupId": "1 1 381C3",
"groupTitle": "South Park",
"isRecording": false,
"numItems": 10,
"numNewItems": 10,
"programs": [
{
"callsign": "COMHD",
"contentId": "1 1 381C3 198",
"description": "\"CrÈme Fraiche\" Animation, Comedy. (2010) Trey Parker, Matt Stone. Randy's obsession with the Food Network forces Sharon to explore a new interest of her own. Series.",
"duration": 1800,
"episodeTitle": "CrÈme Fraiche",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6937756",
"rating": "TV-MA-L",
"startTime": 1290056400,
"title": "South Park",
"uniqueId": "5397375192530140068"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 197",
"description": "\"Coon vs. Coon & Friends\" Animation, Comedy. (2010) Trey Parker, Matt Stone. Coon and Friends find themselves at the mercy of Cartman who now has the dark lord doing his bidding; Kenny wrestles with the curse of his super power. Series.",
"duration": 1800,
"episodeTitle": "Coon vs. Coon & Friends",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6918387",
"rating": "TV-MA-L",
"startTime": 1289444400,
"title": "South Park",
"uniqueId": "8476957599452610419"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 196",
"description": "\"Mysterion Rises\" Animation, Comedy. (2010) Trey Parker, Matt Stone. Mysterion's true identity is revealed; scorned by fellow superheroes, one goes out for revenge. Series.",
"duration": 1800,
"episodeTitle": "Mysterion Rises",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6896823",
"rating": "TV-MA-L",
"startTime": 1288843200,
"title": "South Park",
"uniqueId": "2113731123121326471"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 0",
"description": "Animation, Comedy. Trey Parker, Matt Stone. The adventures of four boys who live in South Park, Colo. Series.",
"duration": 1800,
"episodeTitle": "",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6830127",
"rating": "TV-MA-L",
"startTime": 1288751400,
"title": "South Park",
"uniqueId": "2166056034991125062"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 195",
"description": "\"Coon 2: Hindsight\" Animation, Comedy. (2010) Trey Parker, Matt Stone. Coon and friends set out to help the victims of BP's latest catastrophic drilling accident in the Gulf. Series.",
"duration": 1800,
"episodeTitle": "Coon 2: Hindsight",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6876407",
"rating": "TV-MA-L",
"startTime": 1288238400,
"title": "South Park",
"uniqueId": "8615834721021879366"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 194",
"description": "\"Insheeption\" Animation, Comedy. (2010) Trey Parker, Matt Stone. When Stan is sent to the school counselor because he's holding on to an obscene number of useless possessions, he realizes that Mr. Mackey had a hoarding disorder too. Series.",
"duration": 1783,
"episodeTitle": "Insheeption",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6857628",
"rating": "TV-MA-L",
"startTime": 1287626416,
"title": "South Park",
"uniqueId": "1547798106243799673"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 193",
"description": "\"It's a Jersey Thing\" Animation, Comedy. (2010) Trey Parker, Matt Stone. New Jersey is taking over the nation one state at a time, Randy and the boys take a stand as the Jerseyites approach South Park. Series.",
"duration": 1800,
"episodeTitle": "It's a Jersey Thing",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6839243",
"rating": "TV-MA-L",
"startTime": 1287028800,
"title": "South Park",
"uniqueId": "8123622337314668826"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 193",
"description": "\"It's a Jersey Thing\" Animation, Comedy. (2010) Trey Parker, Matt Stone. New Jersey is taking over the nation one state at a time, Randy and the boys take a stand as the Jerseyites approach South Park. Series.",
"duration": 1784,
"episodeTitle": "It's a Jersey Thing",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6839243",
"rating": "TV-MA-L",
"startTime": 1287021615,
"title": "South Park",
"uniqueId": "949183344652655207"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 0",
"description": "Animation, Comedy. Trey Parker, Matt Stone. The adventures of four boys who live in South Park, Colo. Series.",
"duration": 1800,
"episodeTitle": "",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6830127",
"rating": "TV-MA-L",
"startTime": 1286937000,
"title": "South Park",
"uniqueId": "7841891672059226043"
},
{
"callsign": "COMHD",
"contentId": "1 1 381C3 0",
"description": "Animation, Comedy. Trey Parker, Matt Stone. The adventures of four boys who live in South Park, Colo. Series.",
"duration": 1800,
"episodeTitle": "",
"expiration": "0",
"isHd": true,
"isPartial": false,
"isPpv": false,
"isRecording": false,
"isViewed": false,
"isVod": false,
"keepUntilFull": true,
"major": 249,
"minor": 65535,
"programId": "6830127",
"rating": "TV-MA-L",
"startTime": 1286856000,
"title": "South Park",
"uniqueId": "3872435936942587460"
}
]
}
答案 0 :(得分:2)
您是否尝试过其他编码,例如NSISOLatin1StringEncoding?
答案 1 :(得分:1)
这可能是来自Web服务器的编码问题。你期待UTF-8数据,但我敢打赌,这不是他们发送给你的东西。尝试使用[NSString stringWithContentsOfFile:]进行阅读,看看是否有任何变化。
编译器将抛出警告,因为该方法已弃用。事实上,您可以使用stringWithContentsOfFile:usedEncoding:error:相反,但是我和其他人已经报告它似乎没有像旧方法那样完美编码的数据(即90%的网络) : - 。))
答案 2 :(得分:0)
检查DirecTV单元响应中的HTTP标头。它可能不使用UTF-8。找出它正在使用的编码,然后向initWithData:encoding:
提供正确的常量。
您可以使用命令curl -i http://directv-data-url
查看HTTP标头。