一,工程图。
二,代码。
#import "RootViewController.h"
//为JSONKit添加头文件
#import "JSONKit.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=@"JSONKit的使用";
//用JSONKit将字典转化为字符串
NSDictionary* jsonDic = [NSDictionary dictionaryWithObjectsAndKeys:@"李小华",@"name",@"女",@"sex",@"23",@"age",@"BeiJing",@"from",nil];
NSString *jsonString=[jsonDic JSONString];
NSLog(@"-------------jsonDic--%@",jsonDic);
NSLog(@"--------------jsonString--%@-------",jsonString);
//将字符串转为数组
NSArray *jsonArr=[jsonString objectFromJSONString];
NSLog(@"------jsonArr----%@",jsonArr);
}
三,输出。
2015-10-22 14:44:30.160 JSONKit的使用[12162:246560] -------------jsonDic--{
age = 23;
from = BeiJing;
name = "U674eU5c0fU534e";
sex = "U5973";
}
2015-10-22 14:44:30.161 JSONKit的使用[12162:246560] --------------jsonString--{"age":"23","sex":"女","name":"李小华","from":"BeiJing"}-------
2015-10-22 14:44:30.162 JSONKit的使用[12162:246560] ------jsonArr----{
age = 23;
from = BeiJing;
name = "U674eU5c0fU534e";
sex = "U5973";
}