iOS:如何从soap响应中获取数据?

时间:2013-10-29 19:09:34

标签: ios soap response

<?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><IOSAPIProjectResponse xmlns="http://tempuri.org/">
    <IOSAPIProjectResult>{"result":[{"projectcatname":"Test1","projectname":"Sample1","imgname":"http://someurl.com/UploadImages/projects/5.jpg"},{"projectcatname":"Test2","projectname":"Sample2","imgname":"http://someurl.com/UploadImages/projects/6.jpg"}],"status":"SUCCESS"}
    </IOSAPIProjectResult></IOSAPIProjectResponse></soap:Body></soap:Envelope>

我从.asmx API获得以下响应,如何从响应中获得结果?

2 个答案:

答案 0 :(得分:0)

这是一个XML结果,使用NSXMLParser,你应该使用的主要方法是:

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser parse];

检查当前元素:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict;

从这个元素中获取字符串:

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;

答案 1 :(得分:0)

看起来您的回复是用JSON编码的。 iOS内置了JSON解码功能。我建议您阅读NSJSONSerialization Class Reference

简而言之,您可以使用以下命令从JSON编码数据中获取NSDictionary对象:

NSError* error;
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:responseData 
options:kNilOptions 
error:&error];

然后使用NSDicitionary对象通过objectForKey获取您要查找的结果。