Der folgende Code, der entworfen wurde, um NSNull in Json-Ergebnissen abzufangen, löst eine Ausnahme aus, wenn die Antwort null ist.IOS: Erkennen von NSNull von JSON funktioniert nicht
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"jsonResults are:%@",jsonResults);
if (![jsonResults isKindOfClass:[NSNull class]]&&!
[jsonResults[@"response"][@"insert_id"]isKindOfClass:[NSNull class]]&&!
(jsonResults==nil)){
//do something
}
Wenn die Ausnahme auftritt, wird die Linie if (![json...
beginnend in grün ist und die Fehlermeldung lautet:
Results: {
code = 400;
response = "0(NSNull)";
}
2016-05-26 07:18:06.327 idaru[385:60b] -[NSNull
objectForKeyedSubscript:]: unrecognized selector sent to instance
0x38871a70
2016-05-26 07:18:06.329 myapp[385:60b] *** Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull
objectForKeyedSubscript:]: unrecognized selector sent to instance
0x38871a70'
Kann jemand empfehlen, was hier falsch sein könnte?
Beachten Sie, ich habe eine Kategorie, die angeblich NSNulls in 0 verwandelt. Nicht sicher, wie dies in Wechselwirkung mit oben, aber hier ist es:
//NSNull+JSON.m
@implementation NSNull (JSON)
- (NSUInteger)length { return 0; }
- (NSInteger)integerValue { return 0; };
- (float)floatValue { return 0; };
- (NSString *)description { return @"0(NSNull)"; }
- (NSArray *)componentsSeparatedByString:(NSString *)separator { return @[]; }
- (id)objectForKey:(id)key { return nil; }
- (BOOL)boolValue { return NO; }
@end
Was bekommen Sie als Antwort? drucke es hier aus. Erhalten Sie leere Zeichenfolge oder null? beide sind unterschiedliche Sache – Lion
Antwort = "0 (NSNull)"; – zztop
Ich frage, was Sie in 'jsonResults' bekommen? – Lion