1 //json 2 //javascript 语法的子集 3 //key : value 类似于dictionary对象 4 //json是由数组和对象构成的 5 6 //1.json文件的最外层结构为字典或是数组,大多数是字典 7 //2.json数据可以是基本数据类型,字符串对象,数组对象([]包含),字典对象,逻辑对象({}包含),null, 8 9 int main(int argc, const char * argv[]) { 10 @autoreleasepool { 11 12 #if 0 13 //1.先将json文件读取位NSData类型的数据 14 //NSData *data = [NSData dataWithContentsOfFile:@""]; 15 NSString *dataString = [NSString stringWithContentsOfFile:@"/Users/qianfeng/Desktop/day21-OC11_时间类/day21_OC11_json文件解析/网络JSON解析.txt" encoding:NSUTF8StringEncoding error:nil]; 16 NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding]; 17 18 //2.解析json数据 19 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 20 21 NSLog(@"%@",dict); 22 #endif 23 } 24 25 @autoreleasepool { 26 //解析json网址 27 //将字符串对象转换为网址 28 NSString *aUrl = @"http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=%E9%93%B6%E9%AD%82&bk_length=600"; 29 NSURL *url = [[NSURL alloc] initWithString:aUrl]; 30 NSString *urlString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 31 32 NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding]; 33 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 34 35 NSLog(@"%@",dict); 36 } 37 return 0; 38 }
时间: 2024-10-19 19:45:47