2013-03-13 2 views
0

iOS에서 대부분의 데이터가 JSON 또는 XML로 표시됩니다. 필자는 두 가지 유형의 데이터와 응답을 적절하게 처리하는 타사 라이브러리 또는 래퍼 클래스가 있는지 여부에 관심이있었습니다. 예를 들어 JSON 데이터를 JSON 데이터로, NSXMLParser를 XML로 사용할 수 있다는 것을 알고 있습니다. 그러나 나는 둘 다를 다루는 사람을 찾고 있습니다.iOS에서 파싱하기위한 JSON 및 XML 래퍼

래퍼가 있습니까?

모든 제안 및 안내에 오신 것을 환영합니다. 감사.

답변

1

JSON/XML에 대한 지원이라고 생각합니다. 몇 줄을 바꿀 수 있습니다.

NSString *[email protected]"type url"; 
    NSString *poststr1 = [NSString stringWithFormat:@"%@",str1]; 
    NSString *posturl1=[NSString stringWithFormat:@" your url json/xml"]; 
    // NSLog(@"city url name %@",posturl1); 

    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",posturl1]]; 
    NSData *postData1 = [poststr1 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData1 length]]; 
    NSMutableURLRequest *request =[[[NSMutableURLRequest alloc] init] autorelease]; 
    [request setHTTPBody:postData1]; 
    [request setURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

    NSError *error; 
    NSURLResponse *response; 
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
    // NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];//this line supprted to json 

    NSDictionary* json = [NSJSONSerialization 
          JSONObjectWithData:urlData 

          options:kNilOptions 
          error:&error];//this line supported to xml 
    NSLog(@"json %@",json); 
    //NSDictionary*results = [data JSONValue];//this line supported to JSON 
    //NSLog(@"results json----->%@",results); 
0

AFNetworking이 두 가지를 모두 수행하고, goot 명성을 가지고, 예를 들어 너무 다른 유용한 정보를 많이 가지고있는 Ray Wenderlich 사이트를 볼 수 있습니다.

0

json에 대해 하나의 래퍼 클래스를 만들었습니다. 거기에, 나는 아래에 주어진 하나의 방법을 만들었습니다, 나는 그것이 당신을 도울 것이라고 생각합니다.

구문 분석 방법 :

-(void)jsonDeserialize:(NSString *)key fromDict:(id)content completionHandler:(void (^) (id parsedData, NSDictionary *fromDict))completionHandler{ 
    if (key==nil && content ==nil) { 
     completionHandler(nil,nil); 
    } 
    if ([content isKindOfClass:[NSArray class]]) { 
     for (NSDictionary *obj in content) { 
      [self jsonDeserialize:key fromDict:obj completionHandler:completionHandler]; 
     } 
    } 
    if ([content isKindOfClass:[NSDictionary class]]) { 
     id result = [content objectForKey:key]; 
     if ([result isKindOfClass:[NSNull class]] || result == nil) { 
      NSDictionary *temp = (NSDictionary *)content; 
      NSArray *keys = [temp allKeys]; 
      for (NSString *ikey in keys) { 
      [self jsonDeserialize:key fromDict:[content objectForKey:ikey] completionHandler:completionHandler]; 
      } 
     }else{ 
      completionHandler(result,content); 
     } 
    } 
} 

방법 전화 :

NSData *content = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Sample" ofType:@"json"]]; 
    NSError *error; 

// 직렬화 된 JSON 데이터를 얻을 ...

id dictionary = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:&error]; 

// 얻을 GetInfo를

 [self jsonDeserialize:@"GetInfo" fromDict:dictionary completionHandler:^(id parsedData, NSDictionary *fromDict) { 
      NSLog(@"%@ - %@",parsedData,fromDict); 
     }]; 
라는 키에 대한 데이터