从JSON输出中提取JSON对象

时间:2012-11-28 22:36:45

标签: ios xcode json

这是我的JSON输出

{"activitiesList":{"auid":"19UG9IZ","comments":"Please enter Description","createDate":"09/19/2012 07:32:56 AM","id":"19UG9IZ","owner":"04256CRM","response":"Missing Response","status":"Assigned","type":"Request Action"}}

下面的代码在我有多个activitiesList而不是上面的输出时有效,它有单个的活动列表

@try {
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSArray* activityListFromSiebel = [json objectForKey:@"activitiesList"]; //2
    NSLog(@" Count is %d", [activityListFromSiebel count]);
    for (int i = 0; i < [activityListFromSiebel count]; i++) {
        //if ([activityListFromSiebel count] > 1) {
            NSDictionary* completeActivity = [activityListFromSiebel objectAtIndex:i];

            ObjActivityData *dataActivity = [[ObjActivityData alloc] init];
            dataActivity.activityUID      = [completeActivity objectForKey:@"auid"];
            dataActivity.activitytype     = [completeActivity objectForKey:@"type"];
            dataActivity.activityStatus   = [completeActivity objectForKey:@"status"];
            dataActivity.activityOpenedDt = [completeActivity objectForKey:@"createDate"];
            dataActivity.activityDueDt    = @"";
            dataActivity.activityDesc     = [completeActivity objectForKey:@"comments"];
            dataActivity.activityResponse = [completeActivity objectForKey:@"response"];
            [Activities addObject:dataActivity];
            dataActivity = nil;
        //}
    }
    [self.tableView reloadData];
} @catch (NSException * e) {
    NSLog(@"catching %@ reason %@", [e name], [e reason]);
} @finally {
    NSLog(@"finally");
}

抛出try / catch错误是NSInvalidArgumentException原因 - [__ NSCFArray length]:无法识别的选择器发送到实例0x68c8e90。 如果输出只是1个数组元素,我应该以不同方式解析JSON输出。感谢帮助

由于

1 个答案:

答案 0 :(得分:0)

这样做,

id obj = [json objectForKey:@"activitiesList"];
NSArray* activityListFromSiebel = nil;

if ([obj isKindOfClass:[NSArray class]]) {
    activityListFromSiebel = (NSArray *)obj;
} else {
    activityListFromSiebel = [NSArray arrayWithObject:obj];
}

问题在于,当您尝试使用代码时,您假设[json objectForKey:@"activitiesList"];是一个数组,但实际上它只是一个字典,只存在一个项目。您可以通过传递for循环并将字典转换为else部分中的ObjActivityData对象,或者只是将其添加为您现在使用的for循环之前的快速修复。

所以你的代码看起来像这样,

NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

id obj = [json objectForKey:@"activitiesList"];
NSArray* activityListFromSiebel = nil;

if ([obj isKindOfClass:[NSArray class]]) {
    activityListFromSiebel = (NSArray *)obj;
} else {
    activityListFromSiebel = [NSArray arrayWithObject:obj];
}

NSLog(@" Count is %d", [activityListFromSiebel count]);
for (int i = 0; i < [activityListFromSiebel count]; i++) {
    //if ([activityListFromSiebel count] > 1) {
        NSDictionary* completeActivity = [activityListFromSiebel objectAtIndex:i];

        ObjActivityData *dataActivity = [[ObjActivityData alloc] init];
        dataActivity.activityUID      = [completeActivity objectForKey:@"auid"];
        dataActivity.activitytype     = [completeActivity objectForKey:@"type"];
        dataActivity.activityStatus   = [completeActivity objectForKey:@"status"];
        dataActivity.activityOpenedDt = [completeActivity objectForKey:@"createDate"];
        dataActivity.activityDueDt    = @"";
        dataActivity.activityDesc     = [completeActivity objectForKey:@"comments"];
        dataActivity.activityResponse = [completeActivity objectForKey:@"response"];
        [Activities addObject:dataActivity];
        dataActivity = nil;
    //}
}
[self.tableView reloadData];