[英]how to disable auto sorting JSON dictionary response from web service call in iOS
[英]how can we abstract json data from SOAP web service response and put it into a dictionary in iOS
我正在使用SOAP Web服务。 我向Web服务发出了请求,并从中获得响应。它看起来如下。
[{"sno":null,"AirportCode":"YQM","Airport":"Moncton Airport","City":"Moncton","Country":"Canada"}]<?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><GetAirportResponse xmlns="http://mobileapi.travelcenter.uk/" /></soap:Body></soap:Envelope>
这是从...
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Received Bytes from server: %lu", (unsigned long)[webData length]);
NSString *servieResponse = [[NSString alloc] initWithBytes: [webData bytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",servieResponse);
}
我想得到这个[{“ sno”:null,“ AirportCode”:“ YQM”,“ Airport”:“ Moncton Airport”,“ City”:“ Moncton”,“ Country”:“ Canada”}] json结果NSArray。我该怎么做
这是我的方法。
这是我的方法
NSString *autenticationkey = [NSString stringWithFormat:@"authkey"];
NSString *acode = @"YQM";
//NSString *empty = @"";
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>"
"<GetAirport xmlns=\"http://mapi.uk/\">"
"<Authkey>%@</Authkey>"
"<AirportCode>%@</AirportCode>"
"</GetAirport>"
"</soap:Body>"
"</soap:Envelope>"
,autenticationkey,acode];
NSData *soapData = [soapMessage dataUsingEncoding:NSUTF8StringEncoding];
NSString *mainurlName = [NSString stringWithFormat:@"http://somuturlop/GetAirport"];
NSURL *getcodeserviceUrl = [NSURL URLWithString:mainurlName];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:getcodeserviceUrl];
NSString *messageLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://mopi.uk" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:@"mpi.uk" forHTTPHeaderField:@"HOST"];
[theRequest addValue:messageLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:soapData];
NSURLConnection *theconnetion = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theconnetion) {
webData = [NSMutableData data];
}
else{
NSLog(@"something wrong");
}
为此,我得到以下响应的字符串。
[{"sno":null,"AirportCode":"YQM","Airport":"Moncton Airport","City":"Moncton","Country":"Canada"}]<?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><GetAirportResponse xmlns="http://mobileapi.travelcenter.uk/" /></soap:Body></soap:Envelope>
定义NSMutableData *webData;
这是将Json转换为Nsdictionary的委托方法:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse");
[webData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"didReceiveData");
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSLog(@"didFailWithError: %@",[error description]);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *jsonError = nil;
NSDictionary *dictionaryJSON = [NSJSONSerialization JSONObjectWithData:webData options:kNilOptions error:&jsonError];
}
您可以尝试以下解决方案
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData: webData encoding:NSUTF8StringEncoding];
NSError *error = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &error];
}
希望对您有帮助。
做完了,这就是解决方案。 删除xml标签并使用该字符串。...从responsestring中删除这样的xml标签,
- (NSString *)stringBystrippingxml:(NSString *)provideString
{
NSRange range;
while ((range = [provideString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
provideString = [provideString stringByReplacingCharactersInRange:range withString:@""];
return provideString;
}
然后使用return ProvideString,然后...
resoponseString = [[NSMutableString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSString *one = [self stringBystrippingxml:resultStringOne];
NSError *jsonError;
NSData *jsonData = [one dataUsingEncoding:NSUTF8StringEncoding];
NSArray *getArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];
这很好用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.