gpt4 book ai didi

IOS:如何检查json对象不为空

转载 作者:行者123 更新时间:2023-11-28 17:57:25 27 4
gpt4 key购买 nike

当某些 json 为 null 时,即使我尝试先检查它,我也会收到以下代码的错误:

编辑:前面的代码:

NSData* data = [NSData dataWithContentsOfURL: kItemsURL];
//previous line grabed data from api.
if (data) {
[self performSelectorOnMainThread:@selector(fetchData:) withObject:data waitUntilDone:YES];
}
- (void)fetchData:(NSData *)jsonFeed {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:jsonFeed options:kNilOptions error:&error];

//Original code provided
if (![[json objectForKey:@"items"] isKindOfClass:[NSNull class]]) {
NSLog(@"got here");
NSLog(@"json%@",json);
latestItems = [[json objectForKey:@"items"]mutableCopy];
}

有没有更好的方法来检查Json不为空?

这是错误输出:

2016-05-03 13:05:43.820 testApp[407:60b] got here
2016-05-03 13:05:43.821 testApp[407:60b] json{
items = "<null>";
}
NSNull mutableCopyWithZone:]: unrecognized selector sent to instance 0x3ac26a70
2016-05-03 13:05:43.825 ChallengeU[407:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull mutableCopyWithZone:]: unrecognized selector sent to instance 0x3ac26a70'

最佳答案

if (![json[@"items"] isEqual:[NSNull null]]) {
//do your stuff in here

}

关于IOS:如何检查json对象不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37013529/

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