我来自JSON
[{ “照片”:空}]
我使用此代码
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];
感谢您的帮助
答案 0 :(得分:37)
我相信大多数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;
…
}
}
答案 1 :(得分:7)
如果有效负载是具有可能值的复杂JSON结构,则宏可能会有所帮助。
#define SET_IF_NOT_NULL(TARGET, VAL) if(VAL != [NSNull null]) { TARGET = VAL; }
和宏可以像
一样引用SET_IF_NOT_NULL(myRecord.name, [jsonData objectForKey:@"name"]);
答案 2 :(得分:5)
没有简单的方法可以解决这个问题,但我这样做的方法是在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];
答案 3 :(得分:0)
尝试检查[jPhoto count]
或[NSNull null]
答案 4 :(得分:0)
希望这会有所帮助。
- (NSMutableDictionary *)jsonCheckforNull:(NSMutableDictionary *)json {
NSMutableDictionary * strongjson = [json mutableCopy];
public class RelayCommand : ICommand
{
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute;
}
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
- (NSMutableArray *)ArrayCheckforNull:(NSMutableArray *)arr {
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;
}