在iOS中进行JSON解析并将结果存储到Array中 [英] JSON parsing in iOS and storing the results into Array

查看:182
本文介绍了在iOS中进行JSON解析并将结果存储到Array中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS中初出茅庐,所以请提出一些天真的问题。所以我正在努力工作.net网络服务。我能够从Web服务获取响应,响应就像beow

I'm fledgling in iOS, so please bare with the naive question. So I'm trying to work .net web service. I'm able to fetch the response from web service, the response is like 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

With the below code I'm able to get the only 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如何解析Json对象 json解析+ iphone 和其他类似的问题,但无法解决我的问题。
如何获取 Zone DoctorName 的值并将其存储在Array中,然后在TableView中显示它?

I have checked many similar questions like Xcode how to parse Json Objects, json parsing+iphone and other similar questions but couldn't solve my problem. How can I get the values of Zone and DoctorName and store it in Array then display it in TableView?

推荐答案

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

You need to collect the content of the <getDoctorListResult> element into an instance variable, so add the following as a private class extension

@interface YourClass ()
{
    NSMutableString *_doctorListResultContent;
}

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

And then collect the element content using the XML parser delegate:

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

最后解析中的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]);
        }

    }
}

这篇关于在iOS中进行JSON解析并将结果存储到Array中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆