iOS XML /JSON 解析

JSON解析

NSURL *url = [NSURL URLWithString:URL_JSON];

NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//系統(tǒng)自帶的json解析

_Dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

//分區(qū)個(gè)數(shù)

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return _Dic.count;

}

分區(qū)行數(shù)

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

NSString *key = [_Dic.allKeys objectAtIndex:section];

return [_Dic[key] count];

}

分區(qū)單元格內(nèi)容

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


靜態(tài)字符串

static NSString *ID = @"cyy";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

}

NSString *Strkey = [_Dic.allKeys objectAtIndex:indexPath.section];

cell.textLabel.text = [[_Dic [Strkey]objectAtIndex:indexPath.row]objectForKey:@"name"];

cell.detailTextLabel.text = [[_Dic [Strkey]objectAtIndex:indexPath.row]objectForKey:@"age"];

return cell;

}

XML解析

- (void)viewDidLoad {? ? [super viewDidLoad];? ? ? ? NSURL *url = [[NSURL alloc]initWithString:SAXURL];? ? //初始化session 對象? ? NSURLSession *session = [NSURLSession sharedSession];? ? //請求URL連接? ? NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {? ? ? ? //sax 解析 初始化NSXMLParser對象? ? ? ? NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];? ? ? ? //設(shè)置代理? ? ? ? parser.delegate = self;? ? ? ? //開始解析? ? ? ? BOOL is = [parser parse];? ? ? ? ? ? ? ? if (is) {? ? ? ? ? ? NSLog(@"解析成功");? ? ? ? }else{? ? ? ? ? ? NSLog(@"解析失敗");? ? ? ? }? ? ? ? ? ? } ];? ? //開始請求? ? [task resume];? ? ? ? ? ? ? ? theTable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];? ? ? ? theTable.delegate = self;? ? theTable.dataSource = self;? ? ? ? ? ? [self.view addSubview:theTable];? ? }//開始解析- (void)parserDidStartDocument:(NSXMLParser *)parser{? ? //初始化數(shù)組? ? _allData = [NSMutableArray array];}//遇到開始標(biāo)簽會自動回調(diào)- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary*)attributeDict{

if ([elementName isEqualToString:@"student"]) {

//初始化一個(gè)Student對象

stu = [[Student alloc]init];

//將數(shù)據(jù)添加到數(shù)組

[_allData addObject:stu];

}

//記錄開始標(biāo)簽的名字

_newElementName = elementName;

}

//遇到內(nèi)容會自動回調(diào) 兩個(gè)標(biāo)簽之間的內(nèi)容 空格和回車都會被當(dāng)做內(nèi)容調(diào)用

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

if ([_newElementName isEqualToString:@"name"]) {

stu.name = string;

}else if ([_newElementName isEqualToString:@"age"]){

stu.age = string;

}else if ([_newElementName isEqualToString:@"gender"]){

stu.gender = string;

}

}

//遇到結(jié)束標(biāo)簽會自動回調(diào)

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName{

_newElementName = nil;

[theTable reloadData];

}

//解析完畢

- (void)parserDidEndDocument:(NSXMLParser *)parser{

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return _allData.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellID = @"cyy";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

}

cell.textLabel.text = [[_allData objectAtIndex:indexPath.row]name];

cell.detailTextLabel.text = [[_allData objectAtIndex:indexPath.row]age];

UILabel *oneLb = [[UILabel alloc]initWithFrame:CGRectMake(270, 0, 80, 40)];

oneLb.text = [[_allData objectAtIndex:indexPath.row]gender];

[cell.contentView addSubview:oneLb];

return cell;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嘀略,一起剝皮案震驚了整個(gè)濱河市诗越,隨后出現(xiàn)的幾起案子二拐,更是在濱河造成了極大的恐慌试浙,老刑警劉巖开缎,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件裸卫,死亡現(xiàn)場離奇詭異哗蜈,居然都是意外死亡协怒,警方通過查閱死者的電腦和手機(jī)涝焙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來孕暇,“玉大人仑撞,你說我怎么就攤上這事⊙希” “怎么了隧哮?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長座舍。 經(jīng)常有香客問我沮翔,道長,這世上最難降的妖魔是什么簸州? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任鉴竭,我火速辦了婚禮歧譬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘搏存。我一直安慰自己瑰步,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布璧眠。 她就那樣靜靜地躺著缩焦,像睡著了一般。 火紅的嫁衣襯著肌膚如雪责静。 梳的紋絲不亂的頭發(fā)上袁滥,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機(jī)與錄音灾螃,去河邊找鬼题翻。 笑死,一個(gè)胖子當(dāng)著我的面吹牛腰鬼,可吹牛的內(nèi)容都是我干的嵌赠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼熄赡,長吁一口氣:“原來是場噩夢啊……” “哼姜挺!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起彼硫,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤炊豪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后拧篮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體词渤,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年串绩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了掖肋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,696評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡赏参,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出沿盅,到底是詐尸還是另有隱情把篓,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布腰涧,位于F島的核電站韧掩,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏窖铡。R本人自食惡果不足惜疗锐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一坊谁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧滑臊,春花似錦口芍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至关划,卻和暖如春小染,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背贮折。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工裤翩, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人调榄。 一個(gè)月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓踊赠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親振峻。 傳聞我的和親對象是個(gè)殘疾皇子臼疫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容