简体   繁体   English

我们如何从SOAP Web服务响应中提取JSON数据并将其放入iOS中的字典中

[英]how can we abstract json data from SOAP web service response and put it into a dictionary in iOS

I'm working with a SOAP web service. 我正在使用SOAP Web服务。 I made the request to the web service and get the response from it.it looks like below. 我向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>

this gets from this... 这是从...

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

I want to get this [{"sno":null,"AirportCode":"YQM","Airport":"Moncton Airport","City":"Moncton","Country":"Canada"}] json result to an NSArray.how can I do it 我想得到这个[{“ sno”:null,“ AirportCode”:“ YQM”,“ Airport”:“ Moncton Airport”,“ City”:“ Moncton”,“ Country”:“ Canada”}] json结果NSArray。我该怎么做

this is my method.... 这是我的方法。

this is my method 这是我的方法

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

for this I get the following response for the string . 为此,我得到以下响应的字符串。

[{"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>

Define NSMutableData *webData; 定义NSMutableData *webData;
Here are the delegate methods to convert Json to Nsdictionary : 这是将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];
}

You can try below solution 您可以尝试以下解决方案

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

    }

Hope it helps you..! 希望对您有帮助。

job done, this is the solution. 做完了,这就是解决方案。 remove xml tags and used that string.... remove xml tags like this from the responsestring, 删除xml标签并使用该字符串。...从responsestring中删除这样的xml标签,

- (NSString *)stringBystrippingxml:(NSString *)provideString
{
    NSRange range;
    while ((range = [provideString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
        provideString = [provideString stringByReplacingCharactersInRange:range withString:@""];

    return provideString;
}

then used return provideString, then ..... 然后使用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];

this works well ... 这很好用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从iOS中的Web服务调用禁用自动排序JSON词典响应 - how to disable auto sorting JSON dictionary response from web service call in iOS 如何使用iOS Swift 3从Soap Web服务方法获取json数组字符串? - How can i get json array string from a soap web service method with ios Swift 3? 为什么 Json 字典总是在 nsdata(Bytes) 中给出响应。如何从 Web 服务获取字典 - why Json Dictionary is Giving the response in nsdata(Bytes) always.How can i Get the Dictionary from web service 如何使用Soap Web Service iOS字典将值存储在数组中 - How to store value in array with dictionary for soap web service ios iOS:如何从肥皂反应中获取数据? - iOS : How to get data from soap response? 请求的数据在iOS的Soap Web Service中为空 - requested data getting null in soap web service from ios 如何从iPhone中的SOAP Web服务响应访问XML数据? - How to access XML data from SOAP web-service response in iPhone? iOS:从ASP .Net Web服务接收JSON响应 - iOS: Receiving JSON response from ASP .Net web service iOS:当数据来自Web服务作为响应时,如何获得多个经度和纬度的位置名称? - iOS: How can i get location names of multiple longitudes and latitudes when data is coming from web service as a response? 从ios发送soap Web服务中的参数 - send parameter in soap web service from ios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM