2014-02-26 115 views
0

如何將this url的響應轉換爲json?我得到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轉換爲NSDictionary或具有與JSON文件匹配結構的NSArray。在此行後面放置一個斷點,然後將鼠標懸停在字典上,直到出現一隻眼睛,單擊該斷點,控制檯將顯示捕獲的內容。 –

+0

是的..我需要一些解釋..我將如何從這一行獲得'ProspectReportResult'的價值? – user3244372

相關問題