Alberto Juarez
Alberto Juarez

Reputation: 444

Convert from NSMutableArray to JSON in Objective C

I have a NSMutableArray with objects and i want convert it to JSON to send Web Service a NSString with it information.

I have SBJSON and JSONKit frameworks, but i can't do it. Im doing it with:

 NSString *JSONData=[mutablearray JSONRepresentation];

 NSString *JSONData=[mutablearray JSONString];

How could I achieve this?

Upvotes: 1

Views: 9699

Answers (2)

nisha vishwakarma
nisha vishwakarma

Reputation: 149

You can use this code:

NSArray *keys = [NSArray arrayWithObjects:@"key",nil];
NSArray *objects = [NSArray arrayWithObjects:object,nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString* jsonString = [jsonDictionary JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

You can now send jsonData to the server.. Hope it helps !

Upvotes: 3

TompaLompa
TompaLompa

Reputation: 949

If you are willing to try another JSON-Library you should try Touch-Json: https://github.com/TouchCode/TouchJSON

and then serialize your array with the following code:

 NSData *data = [[[CJSONSerializer serializer] serializeArray:array error:nil];

Upvotes: 1

Related Questions