最近在搞到入问,如何解析json文件丹锹,使用ios自帶的函數(shù)很容易解析出來(lái),但是對(duì)于中文的問(wèn)題芬失,一直困擾著我楣黍,于是查看了各種相關(guān)資料,最后我的解決方法如下:
1棱烂,讓我找到了一個(gè)兼容ARC機(jī)制的類(lèi)庫(kù)租漂,SBJson,現(xiàn)在地址:https://github.com/stig/json-framework/downloads,最近的一個(gè)版本支持ARC
2哩治,將其下載過(guò)來(lái)后秃踩,引入到項(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