将 Json 解析为提供 "null" 值的对象
Parsing Json to object giving "null" Value
无法将此 json 数据解析为对象。我用其他 URL 尝试过相同的代码,工作正常。请指出我哪里做错了?
-(void)callAPI{
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https:s.json"]];
NSError *error=nil;
id response=[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"%@",[error localizedDescription]);
} else {
NSLog(@"%@",response);}}
输出无法读取数据,因为它的格式不正确。
我为你的问题找到了非常完美的解决方案,效果很好now.Please检查下面的答案
- (void)callAPI
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/2iodh4vg0eortkl/facts.json"]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * data,
NSURLResponse * response,
NSError * error) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"jsonString is: %@", jsonString);
NSData *dataCon = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id jsonVal = [NSJSONSerialization JSONObjectWithData:dataCon options:0 error:nil];
if([jsonVal isKindOfClass:[NSDictionary class]]) {
NSLog(@"The response starts with NSDictionary");
NSArray *arrJsonVal = [jsonVal objectForKey:@"rows"];
NSMutableArray *arrTitle = [[NSMutableArray alloc]init];
NSMutableArray *arrDesc = [[NSMutableArray alloc]init];
NSMutableArray *arrImage = [[NSMutableArray alloc]init];
for(NSDictionary *dict in arrJsonVal) {
NSString *strTitle = [dict objectForKey:@"title"];
NSString *strDesc = [dict objectForKey:@"description"];
NSString *strImage = [dict objectForKey:@"imageHref"];
[arrTitle addObject:strTitle];
[arrDesc addObject:strDesc];
[arrImage addObject:strImage];
}
NSLog(@"arrTitle is - %@",arrTitle);
NSLog(@"arrDesc is - %@",arrDesc);
NSLog(@"arrImage is - %@",arrImage);
}else {
NSLog(@"The response starts with NSArray");
}
}] resume];
}
打印结果为
之后
则数组结果为
终于有结果了
无法将此 json 数据解析为对象。我用其他 URL 尝试过相同的代码,工作正常。请指出我哪里做错了?
-(void)callAPI{
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https:s.json"]];
NSError *error=nil;
id response=[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"%@",[error localizedDescription]);
} else {
NSLog(@"%@",response);}}
输出无法读取数据,因为它的格式不正确。
我为你的问题找到了非常完美的解决方案,效果很好now.Please检查下面的答案
- (void)callAPI
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/2iodh4vg0eortkl/facts.json"]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * data,
NSURLResponse * response,
NSError * error) {
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"jsonString is: %@", jsonString);
NSData *dataCon = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id jsonVal = [NSJSONSerialization JSONObjectWithData:dataCon options:0 error:nil];
if([jsonVal isKindOfClass:[NSDictionary class]]) {
NSLog(@"The response starts with NSDictionary");
NSArray *arrJsonVal = [jsonVal objectForKey:@"rows"];
NSMutableArray *arrTitle = [[NSMutableArray alloc]init];
NSMutableArray *arrDesc = [[NSMutableArray alloc]init];
NSMutableArray *arrImage = [[NSMutableArray alloc]init];
for(NSDictionary *dict in arrJsonVal) {
NSString *strTitle = [dict objectForKey:@"title"];
NSString *strDesc = [dict objectForKey:@"description"];
NSString *strImage = [dict objectForKey:@"imageHref"];
[arrTitle addObject:strTitle];
[arrDesc addObject:strDesc];
[arrImage addObject:strImage];
}
NSLog(@"arrTitle is - %@",arrTitle);
NSLog(@"arrDesc is - %@",arrDesc);
NSLog(@"arrImage is - %@",arrImage);
}else {
NSLog(@"The response starts with NSArray");
}
}] resume];
}
打印结果为
之后
则数组结果为
终于有结果了