Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUTF8StringEncoding returns nil NSString

I'm developing an iOs 4 application with latest SDK and XCode 4.2.

I'm using a web service to retrieve some data, and I receive that data. But, when I do this

NSString *json_string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

receivedData has 944bytes but json_string is nil.

From web service I'm getting this string (copy & pasted from browser window):

{"posts": [ { "ID": "324","fecha": "10/02/2012","titulo": "Preparada para la lluvia","texto": "¡Al mal tiempo buena cara! Eso pensé al vestirme el otro día que hiz...","foto": "2012/02/image20.jpg" } ] }

I have tried to validate JSON here, http://jsonformatter.curiousconcept.com/, but I get it is invalid. Show me this formatted output:

{
   "posts":[
      {
         "ID":"324",
         "fecha":"10/02/2012",
         "titulo":"Preparada para la lluvia",
         "texto":,
         "foto":"2012/02/image20.jpg"
      }
   ]
}

And this errors:

Error:Strings should be wrapped in double quotes.[Code 17, Structure 20]
Error:Invalid characters found.[Code 18, Structure 20]

Any clue about what is happening?

like image 723
VansFannel Avatar asked Mar 14 '12 12:03

VansFannel


1 Answers

make sure your have valid UTF8 data, you check that using functions like,

    NSStringEncoding encoding;
    NSString *pageData = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:NULL];
    NSLog(@"Encoding %d",encoding);(Note: NSUTF8StringEncoding = 4)

Available Encoding,

*/
enum {
    NSASCIIStringEncoding = 1,      /* 0..127 only */
    NSNEXTSTEPStringEncoding = 2,
    NSJapaneseEUCStringEncoding = 3,
    NSUTF8StringEncoding = 4,
    NSISOLatin1StringEncoding = 5,
    NSSymbolStringEncoding = 6,
    NSNonLossyASCIIStringEncoding = 7,
    NSShiftJISStringEncoding = 8,          /* kCFStringEncodingDOSJapanese */
    NSISOLatin2StringEncoding = 9,
    NSUnicodeStringEncoding = 10,
    NSWindowsCP1251StringEncoding = 11,    /* Cyrillic; same as AdobeStandardCyrillic */
    NSWindowsCP1252StringEncoding = 12,    /* WinLatin1 */
    NSWindowsCP1253StringEncoding = 13,    /* Greek */
    NSWindowsCP1254StringEncoding = 14,    /* Turkish */
    NSWindowsCP1250StringEncoding = 15,    /* WinLatin2 */
    NSISO2022JPStringEncoding = 21,        /* ISO 2022 Japanese encoding for e-mail */
    NSMacOSRomanStringEncoding = 30,

    NSUTF16StringEncoding = NSUnicodeStringEncoding,      /* An alias for NSUnicodeStringEncoding */

    NSUTF16BigEndianStringEncoding = 0x90000100,          /* NSUTF16StringEncoding encoding with explicit endianness specified */
    NSUTF16LittleEndianStringEncoding = 0x94000100,       /* NSUTF16StringEncoding encoding with explicit endianness specified */

    NSUTF32StringEncoding = 0x8c000100,                   
    NSUTF32BigEndianStringEncoding = 0x98000100,          /* NSUTF32StringEncoding encoding with explicit endianness specified */
    NSUTF32LittleEndianStringEncoding = 0x9c000100        /* NSUTF32StringEncoding encoding with explicit endianness specified */
};
typedef NSUInteger NSStringEncoding;
like image 173
Vignesh Avatar answered Sep 23 '22 09:09

Vignesh