最近在搞到,如何解析json文件,使用ios自帶的函數(shù)很容易解析出來,但是對于中文的問題,一直困擾著我,于是查看了各種相關(guān)資料,最后我的解決方法如下:
1,讓我找到了一個兼容ARC機(jī)制的類庫,SBJson,現(xiàn)在地址:https://github.com/stig/json-framework/downloads,最近的一個版本支持ARC
2,將其下載過來后,引入到項(xiàng)目中,#import "SBJson.h"
3,使用方法是
NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];
NSError *error = nil;
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *rootDic = [parser objectWithString:jsonString error:&error];
NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];
txtView.text = [NSString stringWithFormat:@"今天是 %@? %@? %@? 的天氣狀況是:%@? %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectF