iOSJsonKit的使用

来源:互联网 发布:汽车租赁源码 编辑:程序博客网 时间:2024/06/08 00:19

转自:http://blog.csdn.net/cuiweijie3/article/details/9174739

ios开发中JSONKit的使用

 

C代码  收藏代码
  1. NSLog(@"打印测试");  
  2.   
  3.     NSString *jsonstring =@"[{\"age\":18,\"book\":{\"price\":23.2,\"title\":\"boook111\"},\"name\":\"samyou\"},{\"age\":22,\"book\":{\"price\":33,\"title\":\"booook222\"},\"name\":\"samsam\"}]";  
  4.   
  5.     NSData *data=[jsonstring dataUsingEncoding:NSUTF8StringEncoding];  
  6.   
  7.     NSArray *arr=(NSArray *)[data mutableObjectFromJSONData];  
  8.   
  9.     NSLog(@"count=%d",arr.count);  
  10.   
  11.     for(int i=0;i<arr.count;i++)  
  12.   
  13.     {  
  14.   
  15.         NSDictionary *people=[arr objectAtIndex:i];  
  16.   
  17.         NSString *name=[people objectForKey:@"name"];  
  18.   
  19.         NSString *age=[people objectForKey:@"age"];  
  20.   
  21.         NSLog(@"person withname=%@,age=%d",name,[age intValue]);  
  22.   
  23.         NSDictionary *book=[people objectForKey:@"book"];  
  24.   
  25.         NSString *bookname=[book objectForKey:@"title"];  
  26.   
  27.         NSNumber *price=[book objectForKey:@"price"];  
  28.   
  29.         NSLog(@"book with title=%@,price=%f",bookname,[price doubleValue]);  
  30.   
  31.     }  

 

 

 

C代码  收藏代码
  1. //比如 strJson 是网络上接收到的 json 字符串,  
  2.   
  3.  #import "JSONKit.h"  
  4. NSString *strJson = @"{\"aps\": {\"alert\":{\"body\":\"a msg come!\"},\"bage\":3,\"sound\":\"def.mp3\"}}";  
  5. NSDictionary *result = [jsonData  objectFromJSONData];  
 
C代码  收藏代码
  1. NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];  
  2. NSMutableDictionary *alert = [NSMutableDictionary dictionary];  
  3. NSMutableDictionary *aps = [NSMutableDictionary dictionary];  
  4. [alert setObject:@"a msg come!" forKey:@"body"];  
  5. [aps setObject:alert forKey:@"alert"];  
  6. [aps setObject:@"3" forKey:@"bage" ];  
  7. [aps setObject:@"def.mp3" forKey:@"sound"];  
  8. [jsonDic setObject:aps forKey:@"aps"];  
  9. NSString *strJson = [jsonDic JSONString];  
 

 

 

用法:

1.dictionary------>json

NSString *jsonstring = [dictionary JSONString];

 

 

2.json------------>dictionary

NSDictionary *dictionary = [jsonstring objectFromJSONString];

0 0
原创粉丝点击