如何检查 json 是否为 null?
这里我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相信大多数 JSON 解析器将
null
表示为[NSNull null]
。考虑到
jsonDict
指向数组中的单个元素,那么以下内容应该有效:根据注释进行编辑: 因此
jsonDict
,尽管它的名称,是一个数组。在这种情况下,请将jsonDict
重命名为jsonArray
以避免进一步混淆。然后,考虑jsonArray
指向一个类似于问题中发布的示例的数组:根据OP修改的问题进行进一步编辑:
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:Edit based on comment: so
jsonDict
, despite its name, is an array. In that case, renamejsonDict
tojsonArray
to avoid further confusion. Then, consideringjsonArray
points to an array similar to the example posted in the question:Further edit based on OP’s modified question:
如果有效负载是具有可能值的复杂 JSON 结构,则宏会很有帮助。
和宏可以像这样引用
Macros can be helpful if payload is complex JSON structure having possible values.
and macro can be referenced like
没有简单的方法来处理这个问题,但我的方法是在 NSObject 上创建一个类别:
实现如下:
然后你可以将
notNull
发送到 JSON 字典中的任何可选对象,然后你'将得到nil
如果是 NSNull 则返回。否则你会得到原始对象。例如:
There's no easy way of dealing with this, but the way I do it is to make a category on NSObject:
Implemented like so:
Then you can send
notNull
to any optional object in a JSON dict and you'll getnil
back if it's NSNull. Otherwise you get the original object. For example:
尝试检查
[jPhoto count]
或[NSNull null]
try to check for
[jPhoto count]
or[NSNull null]
希望这会有所帮助。
-(NSMutableDictionary *)jsonCheckforNull:(NSMutableDictionary *)json{
NSMutableDictionary* Strongjson=[json mutableCopy];
}
-(NSMutableArray *)ArrayCheckforNull:(NSMutableArray *)arr{
}
Hope so this helps.
-(NSMutableDictionary *)jsonCheckforNull:(NSMutableDictionary *)json{
NSMutableDictionary* strongjson=[json mutableCopy];
}
-(NSMutableArray *)ArrayCheckforNull:(NSMutableArray *)arr{
}