字符串转换为数组Ios,通过Web服务获取

时间:2014-10-28 12:36:59

标签: ios nsarray

在ios中我正在使用Xml soap解析这是我的回复

<InsertResponse xmlns="http://OrderMe.solyn.in/"><InsertResult>[{"Result":"Success","AccessToken":"f60da1f1-40d7-483d-880a-82348dc20934","AppUserId":"35"}]</InsertResult></InsertResponse>

然后我使用此代码获取响应

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string {
    if (elementFound) {
       // NSLog(@"%@",soapResults);
        [soapResults appendString: string];
        if ([Type isEqualToString:@"InsertResponse"] ) {
            //--- Retrieving text of an element that is found---
            NSLog(@"%@",string);
            NSString *str = [[NSString alloc]initWithData:string encoding:NSUTF8StringEncoding];
            NSArray *allData = [str JSONValue];

            //NSString *loginID=[NSString stringWithFormat:@"%@",string];
            //NSLog(@"Login ID Returned from web service is : %@",loginID);
        }
    }
}

在此代码中** NSLog(@“%@”,string); **此字符串为print

[{"Result":"Success","AccessToken":"f60da1f1-40d7-483d-880a-82348dc20934","AppUserId":"35"}]

所以我不知道如何在数组中转换此字符串 我在回应 请分享您的宝贵知识 问候, Nishant Chandwani 谢谢 。

2 个答案:

答案 0 :(得分:1)

您需要将此字符串解析为JSON:

NSString *string = @"[{\"Result\":\"Success\",\"AccessToken\":\"f60da1f1-40d7-483d-880a-82348dc20934\",\"AppUserId\":\"35\"}]";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

答案 1 :(得分:0)

首先这是xml解析器。那么我们如何从这里获取值。让我们来看下面的编码

在你的.h部分

//步骤1:添加委托类

     First of all you should add <NSXMLParserDelegate>

//第2步:创建必要的对象

     NSXMLParser *parser;
     NSMutableData *ReceviedData;
     NSMutableString *currentStringValue;

     NSMutableArray *arrayResult;
     NSMutableArray *arrayAccessToken;
     NSMutableArray *arrayAppUserId;

在你的.m部分

 //Step 3 - Allocate your all  Arrays in your viewDidLoad method

     arrayAppUserId = [NSMutableArray alloc]init];
     arrayResult = [NSMutableArray alloc]init];
     arrayAccessToken = [NSMutableArray alloc]init];

 //Step 4 - Create Connection in your viewDidLoad Like 

     [self createConnection:@"http://www.google.com"];//give your valid url.

-(void)createConnection:(NSString *)urlString
{
     NSURL *url = [NSURL URLWithString:urlString];

  //Step 5 - parser delegate methods are using NSURLConnectionDelegate class or not.
    BOOL success;
    if (!parser)
    {
      parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
      parser.delegate = self;
      parser.shouldResolveExternalEntities = YES;
      success = [parser parse];
      NSLog(@"Success : %c",success);
    }
  }

   -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
  {
      NSLog(@"Current Element Name : %@",elementName);

     if ([elementName isEqualToString:@"Result"])
     {
         NSLog(@"The Result is==%@",elementName);
     }
     if ([elementName isEqualToString:@"AccessToken"])
     {
         NSLog(@"The AccessToken is==%@",elementName);
     }
     if ([elementName isEqualToString:@"AppUserId"])
     {
         NSLog(@"The appUserId is==%@",elementName);
     }
  }

  -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  {
    currentStringValue = [[NSMutableString alloc] initWithString:string];
     NSLog(@"Current String Value : %@",currentStringValue);
  }

  -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
   {
     if ([elementName isEqualToString:@"Result"])
     {
       [arrayResult addObject:currentStringValue];
     }
     if ([elementName isEqualToString:@"AccessToken"])
     {
       [arrayAccessToken addObject:currentStringValue];
     }
     if ([elementName isEqualToString:@"AppUserId"])
     {
       [arrayAppUserId addObject:currentStringValue];
     }
      currentStringValue = nil;
  }