如何查看如果我从json中获取null? [英] How can I check If I got null from json?

查看:117
本文介绍了如何查看如果我从json中获取null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自JSON

[{photo:null}]

[{"photo":null}]

我使用此代码

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

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

How can I check it if I want to use 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解析器

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];

感谢帮助

推荐答案

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

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

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

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

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






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


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
    }
}






根据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;
        …
    }
}

这篇关于如何查看如果我从json中获取null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆