如何检查来自 json 的值是否为 null?

发布于 2024-11-16 06:03:36 字数 832 浏览 2 评论 0原文

我想检查 json 的结果是否为 null ..我使用了此代码

- (NSString *)likesCount {
    if ([facebook valueForKey:@"likes"] == [NSNull null]) {

        return @"";
    }
    else {

    return [facebook valueForKey:@"likes"];

    }
}

likes = 1;如果它存在且为 null "like" 在输出中不可见。但如果它不存在,则返回 (null) ..我在上面使用过代码,但它不起作用。

我正在 UItableviewcell 中打印这个点赞值,

UILabel *commentLike = [[UILabel alloc] initWithFrame:CGRectMake(200, 70, 100, 20)];
commentLike.font = [UIFont fontWithName:@"Arial" size:10.0];

NSString *commentLikeString = [NSString stringWithFormat:@"%@ ",[(Facebook *)[tableArray objectAtIndex:indexPath.row]likesCount]];
    commentLike.text = [NSString stringWithFormat:@"%@ Person"];
    [cell.contentView addSubview:commentLike];

谢谢

I want to check result of json is null or not ..I used this code

- (NSString *)likesCount {
    if ([facebook valueForKey:@"likes"] == [NSNull null]) {

        return @"";
    }
    else {

    return [facebook valueForKey:@"likes"];

    }
}

likes = 1; if it exists and if it is null "like" is not visible in output.. But if it is not present then it is returning (null) .. I used above code but it is not working.

I am printing this value of likes in UItableviewcell

UILabel *commentLike = [[UILabel alloc] initWithFrame:CGRectMake(200, 70, 100, 20)];
commentLike.font = [UIFont fontWithName:@"Arial" size:10.0];

NSString *commentLikeString = [NSString stringWithFormat:@"%@ ",[(Facebook *)[tableArray objectAtIndex:indexPath.row]likesCount]];
    commentLike.text = [NSString stringWithFormat:@"%@ Person"];
    [cell.contentView addSubview:commentLike];

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画中仙 2024-11-23 06:03:36

在 NSDictionary 上使用这个类别,代码会干净很多:

TouchJSON,处理 NSNull

Use this category on NSDictionary, code will be much cleaner:

TouchJSON, dealing with NSNull

书信已泛黄 2024-11-23 06:03:36

试试这个代码

- (NSString *)likesCount {
    if ([[facebook valueForKey:@"likes"] isEqualToString:@"(null)"] || [facebook valueForKey:@"likes"] == nil) {

        return @"";
    }
    else {

    return [facebook valueForKey:@"likes"];

    }
}

Try this code

- (NSString *)likesCount {
    if ([[facebook valueForKey:@"likes"] isEqualToString:@"(null)"] || [facebook valueForKey:@"likes"] == nil) {

        return @"";
    }
    else {

    return [facebook valueForKey:@"likes"];

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文