1

我在通过特定的“”获取 json 格式时遇到问题。我可以看到它是一本字典,所以我把字典变成了一个数组是错误的吗?我正在尝试提取“isReserevble=true”的记录,然后根据用户从 UIDatepicker 中选择的时间在表格视图单元格中显示“开始”。json 是通过 NSlog 实现的,但我无法弄清楚这一点。谢谢

看起来我有一堆字典。我还会使用相同的方法吗?

这是我的代码。

- (void)viewDidLoad
{
    [super viewDidLoad];
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: 
                        kLatestKivaLoansURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) 
                               withObject:data waitUntilDone:YES];

    });

}
- (void)fetchedData:(NSData *)responseData {
//parse out the json data

NSError* error;

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

NSArray* myslots =[json objectForKey:@"slots"];
NSLog(@"allslots: %@", myslots);


  NSMutableArray *datesArray = [[NSMutableArray alloc] init];
for (NSDictionary *slots in json){
    NSLog(@"isReservable = %@",[myslots objectForKey:@"isReservable"]);
    if ([[myslots objectForKey:@"isReservable"]isEqualToString:@"1"]) 
    {
        NSLog(@"begin = %@",[myslots objectForKey:@"begin"]);
        [datesArray addObject:[myslots objectForKey:@"begin"]];
        NSLog(@"slots array count = %d",[datesArray count]);
    }

}
NSLog(@"This is the begin: %@", datesArray);

}

这是我的 NSLog 所有插槽的结果:

2012-08-29 11:54:26.531 GBSB[1137:15b03] allslots: {
    "2012-08-29 00:00:00 America/Los_Angeles" =     (
                {
            begin = "2012-08-29 00:00:00 America/Los_Angeles";
            end = "2012-08-29 08:00:00 America/Los_Angeles";
            isPending = 0;
            isReservable = 0;
            isReserved = 0;
            label = " ";
            span = 1;
        },
                {
            begin = "2012-08-29 08:00:00 America/Los_Angeles";
            end = "2012-08-29 08:15:00 America/Los_Angeles";
            isPending = 0;
            isReservable = 1;
            isReserved = 0;
            label = " ";
            span = 1;
        }
    );
}

好的:这就是我现在得到的

2012-08-30 09:28:30.812 GBSB[835:15b03] its a dictionary
2012-08-30 09:28:30.812 GBSB[835:15b03] isReservable = (null)
2012-08-30 09:28:30.812 GBSB[835:15b03] isReservable = (null)
2012-08-30 09:28:30.812 GBSB[835:15b03] isReservable = (null)
2012-08-30 09:28:30.813 GBSB[835:15b03] This is the begin: (
)
4

2 回答 2

1

错误消息 - 再次 - 有帮助。试着解释一下:

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x81e1c80

这意味着,您将objectForKey:消息发送到 JSON字符串对象而不是解析的 NSDictionary对象,这就是问题所在。

于 2012-08-29T17:31:39.220 回答
0

正是这一点给你带来了问题:

for (NSDictionary *tempDict in self.json) {
    [datesArray addObject:[tempDict objectForKey:@"slots"]];
}

你认为你在询问"slots"你的 JSON 的属性,但你不是,你在询问JSON 中每个子字典"slots"的属性。

试试这个:

[datesArray addObject:[self.json objectForKey:@"slots"]];
于 2012-08-29T17:02:09.180 回答