Json數(shù)據(jù)操作
-
使用
NSDictionary//創(chuàng)建一個字典 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2", nil]; //或者是可以這樣創(chuàng)建字典 NSDictionary *dict = @{@"key1":@"value1",@"key2":@"value2"}; //創(chuàng)建一個 NSData 類型的東西,這就是Json數(shù)據(jù) NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; //將Json數(shù)據(jù)轉(zhuǎn)換為字典 NSDictionary *dict1 = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@",dict1); -
使用
NSArray//從plist文件中讀取一個數(shù)組 NSArray *arr = [NSArray arrayWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"testlist" ofType:@"plist"]]]; //檢查數(shù)組是否符合Json化的要求 if ([NSJSONSerialization isValidJSONObject:arr]) { NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil]; NSLog(@"%@",jsonData); }