Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

objective c - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...