我收到一个 JSON 字符串,我需要迭代该字符串来检索一些对象值。 这是结构
-meta -objects |_cabdriver |_employee |client
There are objects under the objects tree and there are also child nodes, like cabdriver and client. The child node cabdriver has also another child node called employee.
This is the way I am iterating it:
NSArray *messageArray = [json objectForKey:@"objects"];
historialServicios = [[NSMutableArray alloc]init];
// Parse and loop through the JSON
for (dictionary in messageArray) {
//datos de nivel objects
NSString * date = [dictionary objectForKey:@"date"];
NSString * origin = [dictionary objectForKey:@"origin"];
NSString * destiny = [dictionary objectForKey:@"destiny"];
NSString * rate = [dictionary objectForKey:@"service_rate"];
NSString * state = [dictionary objectForKey:@"state"];
NSString * time_service = [dictionary objectForKey:@"time_service"];
NSString * id_service = [dictionary objectForKey:@"id"];
//datos de nivel cliente
NSDictionary *level2Dict = [dictionary objectForKey:@"client"];
NSString *client_id = [level2Dict objectForKey:@"id"];
//datos de nivel cabdriver
NSDictionary *cabdriverLevelDict=[dictionary objectForKey:@"cabdriver"];
//datos de nivel employee
NSDictionary *employeeLevelDict = [cabdriverLevelDict objectForKey:@"employee"];
//datos del employee
NSString *driverName = [employeeLevelDict objectForKey:@"name"];
NSString *driverLastname = [employeeLevelDict objectForKey:@"lastname"];
NSString *driverPhone = [employeeLevelDict objectForKey:@"phone"];
NSString *driverId = [employeeLevelDict objectForKey:@"id"];
[historialServicios addObject:@{
@"time_service": time_service,
@"id_service": id_service,
@"rate": rate,
@"destiny": destiny,
@"state": state,
@"origin": origin,
@"client_id":client_id,
@"date": date,
@"driverName":driverName,
@"driverLastname": driverLastname,
@"driverPhone": driverPhone,
@"driverId": driverId
}];
NSLog(@"DESPUES DE ANADIR OBJETOS");
NSLog(@"OBJETO ANADIDO==>TIME SERVICE = %@, ID SERVICE=%@, SERVICE RATE=%@,SERVICE DATE=%@,DESTINY=%@, STATE =%@,CLIENT ID=%@, ORIGIN=%@,DRIVER NAME=%@, DRIVER LASTNAME=%@,DRIVER PHONE=%@, DRIVER ID=%@",time_service,id_service,rate,date,destiny,state,client_id,origin,driverName,driverLastname,driverPhone,driverId);
//insertamos objetos en diccionario historialServicios
}
如果对象具有所有节点,则一切正常,但有时,节点 cabdriver 为空并且没有员工子节点。如果是这种情况,我会抛出异常并且应用程序崩溃。
如何判断节点员工是否不存在并避免出现异常?
谢谢。
最佳答案
您可以声明一个类别来处理注入(inject)到 json 中的 [NSNull null]
值。
@interface NSDictionary (NilNull)
- (id)optionalObjectForKey:(id)key;
- (id)optionalObjectForKey:(id)key defaultValue:(id)defaultValue;
@end
@implementation NSDictionary (NilNull)
- (id)optionalObjectForKey:(id)key {
return [self optionalObjectForKey:key defaultValue:nil];
]
- (id)optionalObjectForKey:(id)key defaultValue:(id)defaultValue {
id obj = [self objectForKey:key];
return (obj == [NSNull null] || !obj) ? defaultValue : obj;
}
@end
然后使用它:
NSDictionary *cabdriverLevelDict = [dictionary optionalObjectForKey:@"cabdriver"];
NSDictionary *employeeLevelDict = [cabdriverLevelDict optionalObjectForKey:@"employee"];
您尚未发布异常的内容,但从外观来看,它可能与尝试将 nil
值添加到新字典有关。
然后对所有生成对象的数据查找使用默认值 [NSNull null]
,您将使用这些对象构建最终字典。完整的查找源现在将如下所示:
NSString * date = [dictionary optionalObjectForKey:@"date" defaultValue:[NSNull null]];
NSString * origin = [dictionary optionalObjectForKey:@"origin" defaultValue:[NSNull null]];
NSString * destiny = [dictionary optionalObjectForKey:@"destiny" defaultValue:[NSNull null]];
NSString * rate = [dictionary optionalObjectForKey:@"service_rate" defaultValue:[NSNull null]];
NSString * state = [dictionary optionalObjectForKey:@"state" defaultValue:[NSNull null]];
NSString * time_service = [dictionary optionalObjectForKey:@"time_service" defaultValue:[NSNull null]];
NSString * id_service = [dictionary optionalObjectForKey:@"id" defaultValue:[NSNull null]];
//datos de nivel cliente
NSDictionary *level2Dict = [dictionary optionalObjectForKey:@"client" defaultValue:[NSDictionary dictionary]];
NSString *client_id = [level2Dict optionalObjectForKey:@"id" defaultValue:[NSNull null]];
//datos de nivel cabdriver
NSDictionary *cabdriverLevelDict=[dictionary optionalObjectForKey:@"cabdriver" defaultValue:[NSDictionary dictionary]];
//datos de nivel employee
NSDictionary *employeeLevelDict = [cabdriverLevelDict optionalObjectForKey:@"employee" defaultValue:[NSDictionary dictionary]];
//datos del employee
NSString *driverName = [employeeLevelDict optionalObjectForKey:@"name" defaultValue:[NSNull null]];
NSString *driverLastname = [employeeLevelDict optionalObjectForKey:@"lastname" defaultValue:[NSNull null]];
NSString *driverPhone = [employeeLevelDict optionalObjectForKey:@"phone" defaultValue:[NSNull null]];
NSString *driverId = [employeeLevelDict optionalObjectForKey:@"id" defaultValue:[NSNull null]];
关于ios - 如何检测字典是否为空或为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127460/