NSString *pathfile = [[NSBundle mainBundle] pathForResource:@"weather" ofType:@"json"];
NSData *data = [[NSData alloc] initWithContentsOfFile:pathfile];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSDictionary *dic2 = [dic objectForKey:@"weatherinfo"];
for (NSString *a in [dic2 allValues]) {
NSLog(@"%@",a);
NSJSON直接将二进制流(nsdata)转化成字典1,字典1的value也是字典,设为字典2. 利用for in 可以遍历输出字典2中所有的value值.
其中,我的json格式的文件为weather.json.如下
{"weatherinfo":{"city":"北京","city_en":"beijing","date_y":"2013年12月29日","date":"","week":"星期日","fchh":"11","cityid":"101010100","temp1":"6℃~-6℃","temp2":"7℃~-5℃","temp3":"8℃~-4℃","temp4":"9℃~-4℃","temp5":"8℃~-4℃","temp6":"7℃~-5℃","tempF1":"42.8℉~21.2℉","tempF2":"44.6℉~23℉","tempF3":"46.4℉~24.8℉","tempF4":"48.2℉~24.8℉","tempF5":"46.4℉~24.8℉","tempF6":"44.6℉~23℉","weather1":"晴","weather2":"晴","weather3":"晴","weather4":"晴","weather5":"晴","weather6":"晴","img1":"0","img2":"99","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"99","img9":"0","img10":"99","img11":"0","img12":"99","img_single":"0","img_title1":"晴","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":"晴","img_title6":"晴","img_title7":"晴","img_title8":"晴","img_title9":"晴","img_title10":"晴","img_title11":"晴","img_title12":"晴","img_title_single":"晴","wind1":"微风","wind2":"微风","wind3":"北风3-4级转微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小于3级","fl2":"小于3级","fl3":"3-4级转小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"冷","index_d":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。","index48":"冷","index48_d":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。","index_uv":"中等","index48_uv":"中等","index_xc":"适宜","index_tr":"适宜","index_co":"较舒适","st1":"6","st2":"-4","st3":"7","st4":"-3","st5":"7","st6":"-2","index_cl":"适宜","index_ls":"基本适宜","index_ag":"极不易发"}}