Skip to content

Instantly share code, notes, and snippets.

@mhayashi
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhayashi/f23152624e89f495438e to your computer and use it in GitHub Desktop.
Save mhayashi/f23152624e89f495438e to your computer and use it in GitHub Desktop.
JSON to NSDictionary
NSData *jsonData = [@"{}" dataUsingEncoding:NSUTF8StringEncoding]; // --> {};
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:nil];
jsonData = [@"{\"a\":{}}" dataUsingEncoding:NSUTF8StringEncoding]; // --> { a = {}; }
result = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:nil];
jsonData = [@"{\"a\":false}" dataUsingEncoding:NSUTF8StringEncoding]; // --> { a = 0; }
result = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:nil];
@mhayashi
Copy link
Author

{} is not falsey. We have to check result.count if we want to know whether the dictionary is empty or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment