2010-01-20 2 views
1

Мне нужно проанализировать информацию заголовка ответа и обработать код состояния. Я получил строку ответа html, теперь мне нужно разобрать, как это сделать в iPhone?Parse Html заголовки ответов iPhone

+0

HTML ответа или HTTP repsonse? – kennytm

ответ

0

Посмотрите на следующую информацию в документации:

  • NSString componentsSeparatedByString:
  • NSString componentsSeparatedByCharactersInSet:
  • NSScanner класса
0

Если вы используете NSURLConnection для HTTP вызовов вы можете получить код ответа HTTP и заголовки из методов NSURLConnectionDelegate следующим образом:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; 
    NSInteger statusCode = [httpResponse statusCode]; 
    NSDictionary* headers = [httpResponse allHeaderFields]; 
} 
0
NSMutableDictionary *n=[[NSMutableDictionary alloc]init]; 
    NSString *[email protected]"asd"; 
    NSString *s=[[[[str componentsSeparatedByString:@"<"] objectAtIndex:1] componentsSeparatedByString:@">"]objectAtIndex:0]; 
    NSArray *a = [s componentsSeparatedByString:@"|"]; 
    [n setObject:[a objectAtIndex:0] forKey:@"status"]; 
    if([a count]>1) 
    { 
     [n setObject:[a objectAtIndex:1] forKey:@"session"]; 
     NSArray *b = [[a objectAtIndex:2] componentsSeparatedByString:@"&"]; 
     for(int i=0;i<[b count];i++) 
     { 
      NSArray *arr = [[b objectAtIndex:i ] componentsSeparatedByCharactersInSet:[ NSCharacterSet characterSetWithCharactersInString:@",="]]; 
      NSMutableIndexSet *eset=[[NSMutableIndexSet alloc]init]; 

      NSMutableIndexSet *oset=[[NSMutableIndexSet alloc]init]; 
      for(int j=0;j<[arr count];j=j+2) 
      { 
       [eset addIndex:j]; 
       [oset addIndex:j+1]; 
      } 

      NSArray *names = [arr objectsAtIndexes: eset]; 
      NSArray *values = [arr objectsAtIndexes: oset]; 
      NSDictionary * dict = [NSDictionary dictionaryWithObjects:values forKeys:names]; 
      [n setObject:dict forKey:[dict objectForKey:@"DisplayName"]]; 

     } 

     NSLog(@"%@",n); 

    } 
0

сохранить следующий ответ как sample.json файл

{ 

"Category": [ 

      { 

      "name": "Mobile" 

      }, 

      { 

      "name": "TV" 

      }, 

      { 

      "name": "Computer" 

      }, 

      { 

      "name": "Camera" 

      }, 

      { 

      "name": "Home Appilances" 

      } 

      ] 

} 





NSError *error = nil; 

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"json"]; 

NSData *myData = [NSData dataWithContentsOfFile:filePath]; 



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

NSArray *catArray = [dict objectForKey:@"Category"]; 

for (int index = 0; index < catArray.count; index++) { 

    NSDictionary *valDic = [catArray objectAtIndex:index]; 

    NSLog(@"%@", [valDic objectForKey:@"name"]); 

} 
Смежные вопросы