加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSONKit

发布时间:2020-12-16 19:53:30 所属栏目:百科 来源:网络整理
导读:1.将字典或者数组反序列化为NSString. NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary]; NSMutableDictionary *alert = [NSMutableDictionary dictionary]; NSMutableDictionary *aps = [NSMutableDictionary dictionary]; [alert setObje
1.将字典或者数组反序列化为NSString.
NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
NSMutableDictionary *alert = [NSMutableDictionary dictionary];
NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[alert setObject:@"a msg come!" forKey:@"body"];
[aps setObject:alert forKey:@"alert"];
[aps setObject:@"3" forKey:@"bage" ];
[aps setObject:@"def.mp3" forKey:@"sound"];
[jsonDic setObject:aps forKey:@"aps"];

NSString *strJson = [jsonDic JSONString];

NSLog(@"%@",strJson);


2.将NSString反序列化为数组、字典.
NSDictionary *result = [strJson objectFromJSONString];
或者
NSDictionary *result = [dataJson objectFromJSONData];

NSLog(@"333%@",result);


JSON和Object-C中数据类型的映射关系如下表所示

Number Array Object
JSON Objective-C
null NSNull
trueandfalse NSNumber
String NSString
NSArray
NSDictionary

下面写一个简单的程序使用一下JSONKit(只需下载头文件以及源文件,放在项目目录下

[cpp] view plain copy
  1. #import<Foundation/Foundation.h>
  2. #import"lib/JSONKit.h"
  3. intmain(intargc,constchar*argv[]){
  4. NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];
  5. NSString*res=nil;
  6. /*
  7. *json格式编码
  8. */
  9. //字符串
  10. NSString*str=@"thisisansstring";
  11. res=[strJSONString];
  12. NSLog(@"res=%@",[NSStringstringWithString:res]);
  13. //res="thisisansstring"
  14. //数组
  15. NSArray*arr=[[NSArrayalloc]initWithObjects:@"One",@"Two",@"Three",nil];
  16. res=[arrJSONString];
  17. NSLog(@"res=%@",[NSStringstringWithString:res]);
  18. [arrrelease];
  19. //res=["One","Two","Three"]
  20. //字典类型(对象)
  21. NSArray*arr1=[NSArrayarrayWithObjects:@"dog",@"cat",nil];
  22. NSArray*arr2=[NSArrayarrayWithObjects:[NSNumbernumberWithBool:YES],[NSNumbernumberWithInt:30],nil];
  23. NSDictionary*dic=[NSDictionarydictionaryWithObjectsAndKeys:arr1,@"pets",arr2,@"other",nil];
  24. res=[dicJSONString];
  25. NSLog(@"res=%@",[NSStringstringWithString:res]);
  26. //res={"pets":["dog","cat"],"other":[true,30]}
  27. /*
  28. *json格式解码
  29. */
  30. JSONDecoder*jd=[[JSONDecoderalloc]init];
  31. //针对NSData数据
  32. NSData*data=[dicJSONData];
  33. NSDictionary*ret=[jdobjectWithData:data];
  34. NSLog(@"res=%@",[retobjectForKey:@"pets"]);
  35. //res=(
  36. //dog,
  37. //cat
  38. //)
  39. NSLog(@"res=%@",[[retobjectForKey:@"other"]objectAtIndex:0]);
  40. //res=1
  41. //针对NSString字符串数据
  42. NSString*nstr=[dicJSONString];
  43. NSDictionary*ret2=[jdobjectWithUTF8String:(constunsignedchar*)[nstrUTF8String]length:(unsignedint)[nstrlength]];
  44. NSLog(@"res=%d",[[ret2objectForKey:@"pets"]indexOfObject:@"cat"]);
  45. //res=1
  46. NSLog(@"res=%@",[[ret2objectForKey:@"other"]objectAtIndex:1]);
  47. //res=30
  48. [jdrelease];
  49. [pooldrain];
  50. return0;
  51. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读