-2

i want to generate a JSON in Objective C , below is an example, one dictionary which has some fields, and within that dictionary i want to add another dictionary. i.e. Student dictionary can have subjects dictionary(which can have multiple items.

{
    "student": {
        "name": "b",
        "std": "d",
        "subjects": [{"id":"1","id":"2","id":"3"}]
}}

I am trying using NSJSONSerialization , i tried first creating json string of subjects dictionary but, after converting final JSON of main dictionary its showing (Slash) which is breaking json. i added subjects inside main dictionary then after converting its showing subjects json starting with (

Any suggestion what m i missing?

Thanks

Suresh
  • 1,131
  • 1
  • 15
  • 28

1 Answers1

6

Use NSJSONSerialization :

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionaryOrArrayToOutput 
                  options:NSJSONWritingPrettyPrinted error:&error];

Reference : https://stackoverflow.com/a/9020923/3222713

And for your "slash" problem : https://stackoverflow.com/a/19651309/3222713

Rahul Patil
  • 2,707
  • 2
  • 21
  • 32
Pipiks
  • 2,018
  • 12
  • 27
  • 8
    Instead of posting a link to another question and quoting an answer from that question, you should vote to close this question as a duplicate. – rmaddy Jan 07 '16 at 16:28