如何检查 json 是否为 null?

发布于 2024-11-08 11:50:03 字数 1514 浏览 0 评论 0原文

这里我从 JSON

[{"photo":null}]

获取并使用此代码,

NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"photo"]];

如果我想使用 if() ,我该如何检查它?

这里编辑

的是 JSON 数据

   [{"photo":
              [{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928459.jpg","title":"test picture","content":"this is description for test picture.\r\n\u8aac\u660e\u6587\u306a\u306e\u306b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb"}
              ,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928115.jpg","title":"nothing","content":"iMirai"}
              ,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1303276769.jpg","title":"iMirai","content":"Staff"}]}
  ]

,这是我的 JSON 解析器

NSError *theError = nil;     
    NSString *URL = [NSString stringWithFormat:@"http://www.yohyeh.com/apps/get_sub_detail.php?id=%@&menu=photo",g_id];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
    NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];   
    NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSDictionary *jsonDict = [string JSONValue];

谢谢帮助

Here I got from JSON

[{"photo":null}]

and I use this code

NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"photo"]];

How can I check it if I want to use if() ??

edit

here is JSON Data

   [{"photo":
              [{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928459.jpg","title":"test picture","content":"this is description for test picture.\r\n\u8aac\u660e\u6587\u306a\u306e\u306b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb"}
              ,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928115.jpg","title":"nothing","content":"iMirai"}
              ,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1303276769.jpg","title":"iMirai","content":"Staff"}]}
  ]

and here is my JSON parser

NSError *theError = nil;     
    NSString *URL = [NSString stringWithFormat:@"http://www.yohyeh.com/apps/get_sub_detail.php?id=%@&menu=photo",g_id];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
    NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];   
    NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSDictionary *jsonDict = [string JSONValue];

Thank for help

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

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

发布评论

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

评论(5

素年丶 2024-11-15 11:50:03

我相信大多数 JSON 解析器将 null 表示为 [NSNull null]

考虑到 jsonDict 指向数组中的单个元素,那么以下内容应该有效:

if ([jsonDict objectForKey:@"photo"] == [NSNull null]) {
    // it's null
}

根据注释进行编辑: 因此 jsonDict,尽管它的名称,是一个数组。在这种情况下,请将 jsonDict 重命名为 jsonArray 以避免进一步混淆。然后,考虑 jsonArray 指向一个类似于问题中发布的示例的数组:

NSArray *photos = [jsonArray valueForKey:@"photo"];
for (id photo in photos) {
    if (photo == [NSNull null]) {
        // photo is null
    }
    else {
        // photo isn't null
    }
}

根据OP修改的问题进行进一步编辑:

NSArray *jsonArray = [string JSONValue];

NSArray *photos = [jsonArray valueForKey:@"photo"];
for (id photo in photos) {
    if (photo == [NSNull null]) {
        // photo is null
    }
    else {
        // photo isn't null. It's an array
        NSArray *innerPhotos = photo;
        …
    }
}

I believe most JSON parsers represent null as [NSNull null].

Considering jsonDict points to that single element in the array, then the following should work:

if ([jsonDict objectForKey:@"photo"] == [NSNull null]) {
    // it's null
}

Edit based on comment: so jsonDict, despite its name, is an array. In that case, rename jsonDict to jsonArray to avoid further confusion. Then, considering jsonArray points to an array similar to the example posted in the question:

NSArray *photos = [jsonArray valueForKey:@"photo"];
for (id photo in photos) {
    if (photo == [NSNull null]) {
        // photo is null
    }
    else {
        // photo isn't null
    }
}

Further edit based on OP’s modified question:

NSArray *jsonArray = [string JSONValue];

NSArray *photos = [jsonArray valueForKey:@"photo"];
for (id photo in photos) {
    if (photo == [NSNull null]) {
        // photo is null
    }
    else {
        // photo isn't null. It's an array
        NSArray *innerPhotos = photo;
        …
    }
}
对你的占有欲 2024-11-15 11:50:03

如果有效负载是具有可能值的复杂 JSON 结构,则宏会很有帮助。

#define SET_IF_NOT_NULL(TARGET, VAL) if(VAL != [NSNull null]) { TARGET = VAL; }

和宏可以像这样引用

SET_IF_NOT_NULL(myRecord.name, [jsonData objectForKey:@"name"]);

Macros can be helpful if payload is complex JSON structure having possible values.

#define SET_IF_NOT_NULL(TARGET, VAL) if(VAL != [NSNull null]) { TARGET = VAL; }

and macro can be referenced like

SET_IF_NOT_NULL(myRecord.name, [jsonData objectForKey:@"name"]);
半衬遮猫 2024-11-15 11:50:03

没有简单的方法来处理这个问题,但我的方法是在 NSObject 上创建一个类别:

@interface NSObject (NotNull)

- (instancetype)notNull;

@end

实现如下:

@implementation NSObject (NotNull)

- (instancetype)notNull
{
    return self;
}

@end

@implementation NSNull (NotNull)

- (instancetype)notNull
{
    return nil;
}

@end

然后你可以将 notNull 发送到 JSON 字典中的任何可选对象,然后你'将得到nil
如果是 NSNull 则返回。否则你会得到原始对象。例如:

self.parentIdentifier = [dictionary[@"parent_id"] notNull];

There's no easy way of dealing with this, but the way I do it is to make a category on NSObject:

@interface NSObject (NotNull)

- (instancetype)notNull;

@end

Implemented like so:

@implementation NSObject (NotNull)

- (instancetype)notNull
{
    return self;
}

@end

@implementation NSNull (NotNull)

- (instancetype)notNull
{
    return nil;
}

@end

Then you can send notNull to any optional object in a JSON dict and you'll get nil
back if it's NSNull. Otherwise you get the original object. For example:

self.parentIdentifier = [dictionary[@"parent_id"] notNull];
江湖彼岸 2024-11-15 11:50:03

尝试检查 [jPhoto count][NSNull null]

try to check for [jPhoto count] or [NSNull null]

尤怨 2024-11-15 11:50:03

希望这会有所帮助。

-(NSMutableDictionary *)jsonCheckforNull:(NSMutableDictionary *)json{

NSMutableDictionary* Strongjson=[json mutableCopy];

for (NSString *ktr in json) {

    NSObject *str=[json objectForKey:ktr];

    if ([str isKindOfClass:[NSArray class]]) {
        if (!(str==[NSNull null])) {
            NSArray *temp = [json allKeysForObject:str];
            str=[[self ArrayCheckforNull:(NSMutableArray*)str]mutableCopy];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:str forKey:key];
        }
        else
        {
            NSArray *temp = [strongjson allKeysForObject:str];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:@"-----" forKey:key];
        }

    }
    else if ([str isKindOfClass:[NSDictionary class]]) {
        if (!(str==[NSNull null])) {
            str=[[self jsonCheckforNull:str]mutableCopy];
            NSArray *temp = [strongjson allKeysForObject:str];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:str forKey:key];
        }
        else
        {
            NSArray *temp = [strongjson allKeysForObject:str];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:@"-----" forKey:key];
        }

    }

    else {

        if (str ==[NSNull null]) {
            NSArray *temp = [strongjson allKeysForObject:str];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:@"----" forKey:key];
        }

    }

}
return strongjson;

}

-(NSMutableArray *)ArrayCheckforNull:(NSMutableArray *)arr{

NSObject *str;
NSMutableArray* strongArray=[[[NSMutableArray alloc]initWithArray:arr]mutableCopy];
for (str in arr)
{

    if ([str isKindOfClass:[NSArray class]]) {
        if (!(str==[NSNull null])) {
            str=[[self ArrayCheckforNull:(NSMutableArray *)str]mutableCopy];
            [strongArray removeObjectAtIndex:0];
            [strongArray addObject:str];
        }
        else
        {
            [strongArray removeObject:str];
            [strongArray addObject:@"----"];

        }

    }
    else if ([str isKindOfClass:[NSDictionary class]]) {
        if (!(str==[NSNull null])) {
            str=[[self jsonCheckforNull:(NSMutableDictionary*)str]mutableCopy];
            [strongArray removeObjectAtIndex:0];
            [strongArray addObject:str];

        }
        else
        {
            [strongArray removeObject:str];
            [strongArray addObject:@"----"];
        }

    }

    else {

        if (str ==[NSNull null]) {
            [strongArray removeObject:str];
            [strongArray addObject:@"----"];
        }


    }

}
return strongArray;

}

Hope so this helps.

-(NSMutableDictionary *)jsonCheckforNull:(NSMutableDictionary *)json{

NSMutableDictionary* strongjson=[json mutableCopy];

for (NSString *ktr in json) {

    NSObject *str=[json objectForKey:ktr];

    if ([str isKindOfClass:[NSArray class]]) {
        if (!(str==[NSNull null])) {
            NSArray *temp = [json allKeysForObject:str];
            str=[[self ArrayCheckforNull:(NSMutableArray*)str]mutableCopy];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:str forKey:key];
        }
        else
        {
            NSArray *temp = [strongjson allKeysForObject:str];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:@"-----" forKey:key];
        }

    }
    else if ([str isKindOfClass:[NSDictionary class]]) {
        if (!(str==[NSNull null])) {
            str=[[self jsonCheckforNull:str]mutableCopy];
            NSArray *temp = [strongjson allKeysForObject:str];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:str forKey:key];
        }
        else
        {
            NSArray *temp = [strongjson allKeysForObject:str];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:@"-----" forKey:key];
        }

    }

    else {

        if (str ==[NSNull null]) {
            NSArray *temp = [strongjson allKeysForObject:str];
            NSString *key = [temp objectAtIndex:0];
            [strongjson removeObjectForKey:key];
            [strongjson setObject:@"----" forKey:key];
        }

    }

}
return strongjson;

}

-(NSMutableArray *)ArrayCheckforNull:(NSMutableArray *)arr{

NSObject *str;
NSMutableArray* strongArray=[[[NSMutableArray alloc]initWithArray:arr]mutableCopy];
for (str in arr)
{

    if ([str isKindOfClass:[NSArray class]]) {
        if (!(str==[NSNull null])) {
            str=[[self ArrayCheckforNull:(NSMutableArray *)str]mutableCopy];
            [strongArray removeObjectAtIndex:0];
            [strongArray addObject:str];
        }
        else
        {
            [strongArray removeObject:str];
            [strongArray addObject:@"----"];

        }

    }
    else if ([str isKindOfClass:[NSDictionary class]]) {
        if (!(str==[NSNull null])) {
            str=[[self jsonCheckforNull:(NSMutableDictionary*)str]mutableCopy];
            [strongArray removeObjectAtIndex:0];
            [strongArray addObject:str];

        }
        else
        {
            [strongArray removeObject:str];
            [strongArray addObject:@"----"];
        }

    }

    else {

        if (str ==[NSNull null]) {
            [strongArray removeObject:str];
            [strongArray addObject:@"----"];
        }


    }

}
return strongArray;

}

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