0

i need to convert following NSString to NSDictionary

{"type":"1","number":"100","name":"test","sec":0}

Can anyone help!

Thanks

Tejas
  • 53
  • 1
  • 7
  • 1
    What did you try doing? Is it just JSON like? Will it always be valid JSON? – Wain Feb 04 '14 at 15:47
  • what you showd is already a dictionary – santhu Feb 04 '14 at 15:48
  • I tried converting directly to NSDictionary like : NSMutableDictionary *myResult = (NSMutableDictionary *)strMyDict and then i tried accessing : [myResult valueForKey:@"type"] which gave me following exception : Exception in messageReceived : NSUnknownKeyException : [<__NSCFString 0xa67f910> valueForUndefinedKey:]: this class is not key value coding-compliant for the key type. – Tejas Feb 04 '14 at 15:51

1 Answers1

4

If your JSON is a string, get an NSData object first:

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

Then turn it into an NSDictionary using the NSJSONSerialization class:

NSError* error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
Gavin
  • 8,204
  • 3
  • 32
  • 42