2014-02-26 5 views
0

응답을 this url에서 json으로 변환하려면 어떻게해야합니까? do valueForUndefinedKey:]: this class is not key value coding-compliant for the key ANeighborhood.' 다음 코드에서 NSLog(@"array 5656 = %@", array);의 이스케이프 시퀀스로 인한 오류가 발생했습니다.http 요청을 json으로 변환하십시오.

NSURL * url = [NSURL URLWithString:urlString]; 

    NSError *error; 
    NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 

    if(!error) 
    { 
     NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:Nil]; 

     NSDictionary *dict = [jsonDict valueForKey:@"ProspectReportResult"]; 

     NSString *jsonString = [NSString stringWithFormat:@"%@", dict]; 

     NSLog(@"String 444 = %@", jsonString); 

     NSString *jsonString1=[jsonString stringByAppendingFormat:@"%s","]"]; 
     NSString *[email protected]"["; 
     NSString *jsonString2=[jsonString12 stringByAppendingString: jsonString1]; 
     NSString *[email protected]"}{"; 
     jsonString2= [jsonString2 stringByReplacingOccurrencesOfString:jsonString22 withString:@"}][{"]; 

     NSLog(@"String 999 = %@", jsonString2); 

     //jsonString2 = [jsonString2 stringByReplacingOccurrencesOfString:@"\\" withString:@""]; 
     NSArray *array12=[jsonString2 componentsSeparatedByString: @"]"]; 
     NSLog(@"Aray2323 = %@", array12); 
     for(NSString* str in array12) 
     { 
      NSString *str1=[str stringByAppendingFormat:@"%s","]"]; 
      str1 = [str1 stringByReplacingOccurrencesOfString:@"\\" withString:@""]; 
      NSArray *array = [NSArray arrayWithObject:str1]; 
      NSLog(@"array 5656 = %@", array); 

      for (NSDictionary *dict in array) { 
       NSLog(@"Dictionary :%@",dict); 
       prospect = [[ProspectData alloc]init]; 
       [prospectArray addObject:prospect]; 
       prospect.ANeighborhood= (NSString*)[dict valueForKey:@"ANeighborhood"]; 
       prospect.BClientName= (NSString*)[dict valueForKey:@"BClientName"]; 
       prospect.CSoldStatus=(NSString*)[dict valueForKey:@"CSoldStatus"]; 
       prospect.DFirstVisit=(NSString*)[dict valueForKey:@"DFirstVisit"]; 
       prospect.ELastVisit=(NSString*)[dict valueForKey:@"ELastVisit"]; 
       prospect.FNoOfVisit=(NSString*)[dict valueForKey:@"FNoOfVisit"]; 
       prospect.GTypeOfLead=(NSString*)[dict valueForKey:@"GTypeOfLead"]; 
       prospect.HPurchRealtor=(NSString*)[dict valueForKey:@"HPurchRealtor"]; 
       prospect.IClientSource=(NSString*)[dict valueForKey:@"IClientSource"]; 

      } 

     } 
     //[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone]; 
     [self.tableView reloadData]; 
     self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 
    } 
+0

이동은 json.org와 JSON 구문을 공부합니다. 가장 바깥 쪽 구조는 배열이 아닌 "객체"(사전)입니다. 인터넷에서 예를 복사하여 JSON을 처리 할 수 ​​없으므로 이해해야합니다. –

+0

실제로'ProspectReportResult' 키 아래의 JSON 값이 유효하지 않다고 생각합니다. ''{{..}, {..}, ..]'의 배열을','로 구분하여 사용해야합니다. 응답을 수정해야합니다. – Amar

+0

@Amar 그게 내가 NSString 클래스 참조를 사용하여 달성하려했다. 응답을 고칠 정도로 논쟁했습니다. – user3244372

답변

2
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
+1

+1, OP는 새로운 사용자와 유사하므로 몇 가지 설명이 유용 할 것입니다. – Amar

+0

NSJSONSerialization은 Apple의 내장 JSON 파서이며 @iDev로 제안하면 잘 구성된 JSON을 JSON 파일과 일치하는 NSDictionary 또는 NSArray로 변환합니다. 이 줄 바로 뒤에 중단 점을 놓고 눈이 보일 때까지 사전에 마우스를 올려 놓으면 해당 지점을 클릭하면 콘솔에 캡처 된 내용이 표시됩니다. –

+0

예 .. 몇 가지 설명이 필요합니다. 어떻게이 라인에서 'ProspectReportResult'에 대한 가치를 얻을 수 있습니까? – user3244372

관련 문제