Is it possible if I have a NSString and I want to use NSJSONSerialization? How do I do this?
An object that converts between JSON and the equivalent Foundation objects.
A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With