-4

How can i create a json array in below format:

[{"id":"8"},{"id":"9"}]

Using Nsmutable dictionary we can create an above format,but key should be different.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user3388273
  • 401
  • 3
  • 13
  • 1
    possible duplicate of [Generate JSON string from NSDictionary](http://stackoverflow.com/questions/6368867/generate-json-string-from-nsdictionary) – JuJoDi Apr 07 '14 at 12:48

1 Answers1

1
    NSDictionary *dict1 = @{
                            @"id" : @"8"
                            };
    NSDictionary *dict2 = @{
                            @"id" : @"9"
                            };

    NSArray *array = @[dict1,dict2];
    NSData *jsonData=[NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
    NSString* newStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Output

[ { "id" : "8" }, { "id" : "9" } ]

Erhan
  • 908
  • 8
  • 19