我在通过特定“”获取 json 格式时遇到问题。我可以看到它是一本字典所以我把字典变成数组错了吗?我正在尝试提取“isReserevble=true”的记录,然后根据用户从 UIDatepicker 中选择的时间在 TableView 单元格中显示“开始”。 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: (
)
最佳答案
错误消息再次很有帮助。尝试解释它:
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x81e1c80
这意味着,您将 objectForKey:
消息发送到 JSON string 对象而不是 parsed NSDictionary 对象,这是错误的。
关于ios - NSJSON如何解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12182914/