gpt4 book ai didi

ios - 将http请求转成json

转载 作者:行者123 更新时间:2023-11-28 22:14:33 26 4
gpt4 key购买 nike

如何转换来自 this url 的响应到json?我得到 valueForUndefinedKey:]: 此类不符合键 ANeighborhood 的键值编码。' 由于 NSLog(@"array 5656 = %@", array) 中的转义序列而出错; 在下面的代码中这是我尝试过的。

 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 *jsonString12=@"[";
NSString *jsonString2=[jsonString12 stringByAppendingString: jsonString1];
NSString *jsonString22=@"}{";
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];
}

最佳答案

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

关于ios - 将http请求转成json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22034336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com