6

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?

1
  • try removing the square brackets([]) and/or adding an empty string after texto("texto":"",) Commented Mar 14, 2012 at 12:20

2 Answers 2

9

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;
Sign up to request clarification or add additional context in comments.

4 Comments

That is why you get nil value. you can check all supported encoding type in header.
But, how can I get its real name? I need to know its name.
Yes, this is why I am asking you about its name.
No known instance method for selector 'usedEncoding:'
1

I think your problem is the web service that doesn't return valid UTF-8 text. You should check the encoding on the server side.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.