فشل تحويل NSMutabledata إلى NSString إذا كانت هناك شخصية خاصة موجودة

StackOverflow https://stackoverflow.com/questions/4307455

  •  29-09-2019
  •  | 
  •  

سؤال

أنا أكتب تطبيقًا للتفاعل مع جهاز استقبال DIRECTV من خلال واجهة خادم HTTP. أنا قادر على الاستعلام عن الجهاز والحصول على استجابة 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];

يفشل إذا كانت الشخصية التالية في مكانها. È كما في كريم فريش. للإشارة إلى أن هذا كان في appodetitle والوصف من تسجيل ساوث بارك.

إذا قمت بتغيير هذه الشخصية إلى حالة علوية فقط "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"
        }
      ]
    }
هل كانت مفيدة؟

المحلول

هل جربت الترميزات الأخرى ، مثل nsisolatin1stringencoding؟

نصائح أخرى

من المحتمل أن تكون مشكلة تشفير من خادم الويب. أنت تتوقع بيانات UTF-8 ، لكنني أراهن أن هذا ليس ما يرسلونه لك. حاول القراءة باستخدام [NSString StringWithContentSoffile:] بدلاً من ذلك ومعرفة ما إذا كان أي شيء يتغير.

سوف يرمي المترجم تحذيرًا لأن هذه الطريقة تم إهمالها. في الواقع ، يمكنك استخدام StringWithContentSoffile: useencoding: خطأ: بدلاً من ذلك ، لكن أنا والآخرون أبلغوا أنه لا يبدو أنه يعمل وكذلك الطريقة القديمة للبيانات غير مشفرة تمامًا (أي 90 ٪ من الويب :-)).

تحقق من رؤوس HTTP في استجابة وحدة DIRECTV. ربما لا تستخدم UTF-8. اكتشف أي ترميز يستخدمه ، ثم قم بتزويد الثابت الصحيح بـ initWithData:encoding:.

يمكنك استخدام الأمر curl -i http: // directv-data-url لرؤية رؤوس HTTP.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top