//***************************從本地解析json資料***************************
NSString *fileStr = [[NSString alloc] initWithContentsOfFile:@"/Users/qianfeng/Desktop/d5/d2/oc7_json/json.txt" encoding:NSUTF8StringEncoding error:nil];
NSData *data = [fileStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dic = %@",dic);
//判斷是否需要多層周遊解析資料
for (id obj in dic) {
for (id obj1 in [dic objectForKey:obj]) {
NSLog(@"%@",[[dic valueForKey:obj] valueForKey:obj1]);
}
}
//***********************從網絡解析json資料****************************
//從網絡擷取位址組合成NSURL位址
NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/atad/101020100.html"];
//讀取這個位址的内容儲存為字元串格式
NSString *fileStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
//将字元串格式資料轉換為nsdata格式
NSData *data = [fileStr dataUsingEncoding:NSUTF8StringEncoding];
//解析這個data資料儲存成字典檔案
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",fileStr);