3

How can I create a basic JSON string? When I assign the the JSON value in NSString I get a semicolon error. How do I create this string without errors? Could you guys help me out? The code is below:

NSString *dam= @" {
    "Id": 23, "Ratings": {
    Picnicvanlig: 4, Barn Valning: 2, Badvanling: 5, etc..:3
}, "UserId": santosh@facebook.com
}";
Perception
  • 79,279
  • 19
  • 185
  • 195
stephensam
  • 91
  • 3
  • 12
  • 1
    Are you new to all of coding as well? – QED Feb 27 '12 at 12:27
  • Glad to help. You do understand where the error came from? Objective-C (really, C in this case) thinks you're ending the string when it sees the second `"` character... you have to tell the compiler which double-quote characters are part of the string by 'escaping' them with a slash in front. – QED Feb 27 '12 at 12:33
  • Duplicated, see http://stackoverflow.com/questions/6368867/generate-json-string-from-nsdictionary – Ricardo Rivaldo Jan 29 '13 at 17:19

1 Answers1

5

Escape your quoted quotes:

NSString *dam= @"{\"Id\": 23, \"Ratings\": {\"Picnicvanlig\" : 4, \"Barn Valning\": 2, \"Badvanling\" : 5, \"etc..\" : 3 }, \"UserId\": \"santosh@facebook.com\"}";

Better yet, read Apple's JSON stuff.

QED
  • 9,803
  • 7
  • 50
  • 87
  • 2
    The string above is still not a JSON string. All of the keys have to be quoted and so does the email address. – JeremyP Feb 27 '12 at 12:53