NSJSONSerialization from NSString NSJSONSerialization from NSString ios ios

NSJSONSerialization from NSString


First you will need to convert your NSString to NSData by doing the following

NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];

then simply use the JSONObjectWithData method to convert it to JSON

id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];


You need to convert your NSString to NSData, at that point you can use the +[NSJSONSerialization JSONObjectWithData:options:error:] method.

NSString * jsonString = YOUR_STRING;NSData * data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];NSError * error = nil;id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];if (!json) {    // handle error}


You can convert your string to NSData by saying:

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

You can then use it with NSJSONSerialization. Note however that NSJSONSerialization is iOS5 only, so you might be better off using a library like TouchJSON or JSONKit, both of which let you work directly with strings anyway, saving you the step of converting to NSData.