在iOS中进行JSON解析并将结果存储到Array中

时间:2014-08-01 06:59:23

标签: ios json

我在iOS中初出茅庐,所以请提出一些天真的问题。所以我正在努力工作.net网络服务。我能够从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);

             } 
     } 

看起来像dc的变量json等于

[
   {

    "Zone": "CENTRAL NORTH",
    "DoctorName": "Dr Ang Kiam Hwee",

  },
  {

    "Zone": "CENTRAL",
    "DoctorName": "Dr Lee Eng Seng",

  }
]

我检查了许多类似的问题,例如Xcode how to parse Json Objectsjson parsing+iphone和其他类似的问题,但无法解决我的问题。 如何获取ZoneDoctorName的值并将其存储在Array中,然后在TableView中显示它?

3 个答案:

答案 0 :(得分:3)

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

@interface YourClass ()
{
    NSMutableString *_doctorListResultContent;
}

然后使用XML解析器委托收集元素内容:

- (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];  
    }
}

最后在 did end元素委托方法中解析JSON:

- (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]);
        }

    }
}

答案 1 :(得分:1)

我建议将“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"];

答案 2 :(得分:0)

for(直流中的id键)

{

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

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

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