我在解析来自Web服务响应的数据时遇到问题。
我调用了一个Web服务并将响应作为NSLog
,但我需要将数据捕获为NSString
。
以下是我的示例代码:
-(void)connection:(NSURLConnection *) connection didReceiveResponse: (NSURLResponse *) response {
}
-(void)connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
NSString *strData;
strData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser parse];
soapResultsString=[[NSMutableString alloc]init];
recordResults =YES;
}
NSString *xmlparserString;
NSMutableString *soapResultsString;
bool recordResults;
-(void)parserDidEndDocument:(NSXMLParser *)parser
{
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName attributes: (NSDictionary *)attributeDict
{
xmlparserString=elementName;
if( [xmlparserString isEqualToString:@"ns:return"])
{
recordResults =YES;
soapResultsString = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if( recordResults )
{
[soapResultsString appendString: string];
NSLog(@"inside%@",string);
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if( [elementName isEqualToString:@"ns:return"])
{
NSLog(@"parser==>%@",parser);
NSLog(@"nameSpaceUrL==>%@",namespaceURI);
NSLog(@"qName==>%@",qName);
NSData *data = [soapResultsString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// NSLog(@"json==>%@",json);
NSArray *planArray = [json objectForKey:@"hello_History"];
NSLog(@"planArrayCount==>%lu",(unsigned long)[planArray count]);
NSMutableArray *dataSource = [NSMutableArray arrayWithCapacity:planArray.count];
for (int i = 0 ; i<[planArray count]; i++)
{
recordResults = NO;
txn_id=[[[json valueForKey:@"hello_History"]valueForKey:@"date"]objectAtIndex:i];
loginId = [[[json valueForKey:@"hello_History"]valueForKey:@"time"]objectAtIndex:i];
}
}
}
问题是我得到输出到这个方法
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
但我的解析方法不在调用
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
是否有其他方法可以捕获字符串