0

when I try to write this JSON:

{"author":"mehdi","email":"email@hotmail.fr","message":"Hello"}

like this in Objective-C:

NSString *myJson=@"{"author":"mehdi","email":"email@hotmail.fr","message":"Hello"}";

it doesn't work. Can someone help me?

Abizern
  • 146,289
  • 39
  • 203
  • 257
user567
  • 3,712
  • 9
  • 47
  • 80
  • Mehdi, if you expect others to spend time helping you out with your problem, then please at least spent enough time on writing your question to get at least your grammar and spelling halfway done. Presenting other a mess that needs to be deciphered first isn't any fun for anybody. Just a thought to consider prior to posting to StackOverflow. – Regexident May 21 '11 at 23:48
  • We do, of course, realize that English is not your first language. But we do appreciate any effort you can make towards making your question as clear as possible. A poorly-worded question from a non-English speaker can be nearly impossible to understand. – Dave DeLong May 21 '11 at 23:51
  • Thanks Dave, comment was of course meant as in "for the sake of your own benefit", and not to be offensive in any way. ;) – Regexident May 21 '11 at 23:58
  • I'm sorry , i'm french and i don't speek very good english , i trie to use google translator , but ok i will do more effort , sorry . – user567 May 22 '11 at 00:07

1 Answers1

1

You need to escape quote characters with a backslash:

NSString *myJson = @"{\"author\":\"mehdi\",\"email\":\"email@hotmail.fr\",\"message\":\"Hello\"}";

Otherwise the compiler will think that your string literal ends right after the first {. The backslashes will not be present as characters in the resulting NSString. They are merely there as hints for the compiler and are removed from the actual string during compilation.

Newbie note: JSON strings that you read directly from a file via Objective C of course do not need any escaping! (JSON itself may need such, but that's about it. No need for additional escaping on the ObjC-side of it.)

Regexident
  • 29,441
  • 10
  • 93
  • 100