如何將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];
}
轉到json.org所和學習JSON語法。最外層的結構是一個「對象」(字典),而不是一個數組。您無法通過簡單地將示例複製到互聯網上來處理JSON,您必須*理解*它。 –
其實我認爲'ProspectReportResult'鍵下的JSON值是無效的。它應該是''{'},{..},..''分隔的數組','',用','隔開。你應該修復這個迴應。 – Amar
@Amar這就是我想通過使用NSString類引用實現的。我認爲足以修復響應 – user3244372