1

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>

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);

             } 
     } 

The variable dc which looks like json is equal to

[
   {

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

  },
  {

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

  }
]

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?

3 Answers 3

3

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

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

and finally parse the JSON in the did end element delegate method:

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

    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. It's working. Now if I have some 20 details coming and I want to display them in TableView. How can I do that?
@Aniruddha Sorry I'm not going to do all your work for you. I appear to be getting nothing out of it; not even an upvote. Good luck!
I had already accepted and up voted it. Anyways thank you. I'm very new to iOS. I will try out myself.
@Aniruddha OK then. You need to collect the zone/doctor name into a array of dictionaries (another instance variable) and once all the XML is parsed call [self.tableView reloadData] and implement the table view datasource/delegate methods to fetch the rows from this array. There are many examples out there to show how to do that.
1

I would recommend storing "dc" as property and use it as UITableView data source.

self.dataSourceDict = dc;

To get values for given cell (in tableView:cellForRowAtIndexPath: method):

//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"];

5 Comments

Thanks for the reply, first I just want to access the DoctorName and Zone values and store it in a string variables or NSArray. Can you help me with that?
You really need the structures as you have them - they are already in an array and inside objects (NSDictionry) per every instance (row). This is the best way to store them if you are planning to display them as UITableView
If I just want to display the Doctor name in Table, then? How can I get access to each DoctorName value?. Let's say I want to store DoctorName and Zone in seperate arrays. So how can I get individual value and put it into an Array?
If I do something like NSString * name = [dc objectForKey:@"DoctorName"] , it is saying [__NSCFString objectForKey:]: unrecognized selector sent to instance 0x9a13400
[[dc objectAtIndex:0] objectForKey:@"DoctorName"] - you have NSArray with NSDictionaries inside
0

for (id key in dc)

{

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

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

Create one model file and store these value into array using that model file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.