Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何检查json是否为空?_Iphone_Objective C_Ios_Json_Nsmutablearray - Fatal编程技术网

Iphone 如何检查json是否为空?

Iphone 如何检查json是否为空?,iphone,objective-c,ios,json,nsmutablearray,Iphone,Objective C,Ios,Json,Nsmutablearray,这是我从JSON得到的 [{“photo”:null}] 我使用这个代码 NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"photo"]]; 如果要使用if(),如何检查它 编辑 下面是JSON数据 [{"photo": [{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/

这是我从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];

感谢帮助

尝试检查
[jPhoto count]
[NSNull null]
我相信大多数JSON解析器将
null
表示为
[NSNull]

考虑到
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;
        …
    }
}

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

#define SET_IF_NOT_NULL(TARGET, VAL) if(VAL != [NSNull null]) { TARGET = VAL; }
宏可以像这样引用

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

处理这个问题没有简单的方法,但我的方法是在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 dict中的任何可选对象,您将得到
nil
如果它为空,则返回。否则将获得原始对象。例如:

self.parentIdentifier = [dictionary[@"parent_id"] notNull];
希望这能有所帮助

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

-(NSMUTABLEARRY*)阵列检查FORNULL:(NSMUTABLEARRY*)阵列{


}

您使用哪种JSON解析器?我使用JSON-framework。照片中有很多属性,不仅仅是图像链接,还有很多东西。我不确定我是否正确。但我认为NSMutable是可以的。还是不???请原谅我。:)如果您发布了一个更完整的JSON数据示例以及用于解析JSON数据的代码,这会有所帮助。哦,如果我使用NSMutableArray,我可以在([jPhoto objectAtIndex:0]==[NSNull null]){}的情况下使用它,并且它可以工作。谢谢你的帮助。但我仍在等待巴伐利亚的向导。:)哦,它崩溃了。这里有一个错误-[\uu NSArrayM objectForKey:]:无法识别的选择器发送到实例0x58938e0 2011-05-19 11:10:47.131 iLife[2012:207]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu NSArrayM objectForKey:]:无法识别的选择器发送到实例0x58938e0'这意味着
jsonDict
是一个数组,而不是字典。请参阅我对您的问题的最后一点评论。我想知道您所说的myrecord.Name是什么意思非常好的解决方案,而不是为NSObject创建类别和过多的“如果”条件。投赞成票,@Ramesh投赞成票!尝试使用此宏解析双值时出错
二进制表达式的无效操作数('double _Nullable'和'NSNull*.\u Nonnull')
二进制表达式的无效操作数('double _Nullable'和'void*')
我该如何解决这个问题?喜欢!为了更加安全,我刚刚在方法名中添加了一个三个字母的前缀:)@joerick解决方案正在运行,但我没有理解其中的逻辑。如果可能,请解释其工作流程,这将是有益的。谢谢
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;