在iOS中parsingJSON并将结果存储到Array中

我刚刚在iOS上刚刚起步,所以请原谅我的问题。 所以我正在努力工作.netnetworking服务。 我能够从Web服务获取响应,响应就像beow

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getDoctorListResponse xmlns="http://tempuri.org/"><getDoctorListResult> [ { "Zone": "CENTRAL NORTH", "DoctorName": "Dr Ang Kiam Hwee", }, { "Zone": "CENTRAL", "DoctorName": "Dr Lee Eng Seng", } ] </getDoctorListResult> </getDoctorListResponse> </soap:Body> </soap:Envelope> 

通过下面的代码,我可以得到唯一的JSON

  - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if ([currentElement isEqualToString:@"getDoctorListResult"]) { NSDictionary *dc = (NSDictionary *) string; NSLog(@"Dictionary is = \n%@", dc); } } 

看起来像json的variablesdc等于

 [ { "Zone": "CENTRAL NORTH", "DoctorName": "Dr Ang Kiam Hwee", }, { "Zone": "CENTRAL", "DoctorName": "Dr Lee Eng Seng", } ] 

我已经检查了许多类似的问题,如Xcode如何parsingJson对象 , JSONparsing+ iPhone和其他类似的问题,但无法解决我的问题。 我怎样才能得到ZoneDoctorName的值,并将其存储在数组中,然后显示在TableView?

您需要将<getDoctorListResult>元素的内容收集到实例variables中,因此将以下内容添加为私有类扩展

 @interface YourClass () { NSMutableString *_doctorListResultContent; } 

然后使用XMLparsing器委托收集元素内容:

 - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { self.currentElement = elementName; if ([self.currentElement isEqualToString:@"getDoctorListResult"]) { _doctorListResultContent = [NSMutableString new]; } } - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if ([self.currentElement isEqualToString:@"getDoctorListResult"]) { [_doctorListResultContent appendString:string]; } } 

最后在do end元素委托方法中parsingJSON:

 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"getDoctorListResult"]) { NSError *error = nil; NSData *jsonData = [_doctorListResultContent dataUsingEncoding:NSUTF8StringEncoding]; id parsedJSON = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; if (parsedJSON) { NSAssert([parsedJSON isKindOfClass:[NSArray class]], @"Expected a JSON array"); NSArray *array = (NSArray *)parsedJSON; for (NSDictionary *dict in array) { NSString *zone = dict[@"Zone"]; NSString *doctorName = dict[@"DoctorName"]; // Store in array and then reload tableview (exercise to the reader) } } else { NSLog(@"Failed to parse JSON: %@", [error localizedDescription]); } } } 

我build议将“dc”存储为属性,并将其用作UITableView数据源。

 self.dataSourceDict = dc; 

获取给定单元格的值(在tableView:cellForRowAtIndexPath:方法中):

 //deque cell before that NSDictionary* cellData = [self.dataSourceDict objectAtIndex:indexPath.row]; //assuming cell is cutom class extending UITableViewCell cell.zone = cellData[@"Zone"]; cell.doctorName = cellData[@"DoctorName"]; 

for(id key in dc)

{

 NSString *doctorName = [key objectForKey:@"DoctorName"]; NSString *zone = [key objectForKey:@"Zone"]; } 

创build一个模型文件,并使用该模型文件将这些值存储到数组中。