【代碼筆記】iOS-JSONKit的使用,筆記ios-jsonkit
一,工程圖。
二,代碼。
#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 = "\U674e\U5c0f\U534e"; 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 = "\U674e\U5c0f\U534e"; sex = "\U5973";}