ZGJsonUtils.m 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // ZGJsonUtils.m
  3. // UUTPM
  4. //
  5. // Created by zhuge on 15/1/5.
  6. // Copyright (c) 2015年 UUT. All rights reserved.
  7. //
  8. #import "ZGJsonUtils.h"
  9. //#import "ZGAlert.h"
  10. @implementation ZGJsonUtils
  11. #pragma mark private
  12. #pragma mark public
  13. +(id)stringToObject:(NSString *)str {
  14. NSError *error = nil;
  15. NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
  16. id ret = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  17. if (error) {
  18. NSLog(@"JSON ERROR: %@", [error localizedDescription]);
  19. // [ZGAlert alertWithMessage:[error localizedDescription] window:nil];
  20. return nil;
  21. }
  22. return ret;
  23. }
  24. + (NSString *)dictionaryToString:(NSDictionary *)dict {
  25. NSError *error = nil;
  26. // NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
  27. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
  28. if (error) {
  29. NSLog(@"%@", dict);
  30. NSLog(@"JSON ERROR: %@", [error localizedDescription]);
  31. // [ZGAlert alertWithMessage:[error localizedDescription] window:nil];
  32. return nil;
  33. }
  34. return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  35. }
  36. @end