【发布时间】:2015-07-24 04:32:31
【问题描述】:
如何将jsonDictionary[@"response"][obj][@"color"] 加载到表视图数组中。下面是我正在使用的示例代码,但每次我在return [tableviewArray count]; 遇到严重异常时。请为我的问题提供一些解决方案。
NSError *error;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
// get keys from response dictionary
NSMutableArray * key = [[NSMutableArray alloc] initWithArray:[jsonDictionary[@"response"] allKeys]];
// sort as asending order
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
key = (NSMutableArray *)[key sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
// access inner data from dictonary
for (NSString * obj in key) {
// Here below values I want to apply for tableview array
NSLog(@"%@",jsonDictionary[@"response"][obj][@"color"]);
NSArray *tableviewArray = [jsonDictionary[@"response"][obj][@"color"]];
}
我在下面的 tableview 方法上遇到了严重的异常
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableviewArray count]; // Here am getting bad exception
}
我的 JSON:
{
response: {
RED: {
Color: "red",
color_id: "01"},
GREEN: {
Color: "green",
color_id: "02"}
},
Colorcode: {},
totalcolor: "122"
}
【问题讨论】:
-
你昨天已经问过这个问题,更新它然后得到答案stackoverflow.com/questions/31586794/…
-
没有它不同的问题。在那里我问如何从 JSON 中获取值。在这里我问如何加载到tableview @developer
-
有什么异常?似乎您正在创建
tableviewArray作为循环内的局部变量。一旦循环结束,这将被释放。您需要将结果存储到属性或 iVar 中。您的循环也没有任何意义,因为只有最后一个数组将分配给tableviewArray- 较早的数组引用将被覆盖 -
有什么例外?
-
-[__NSCFString count]:无法识别的选择器发送到实例 0x7966c8b0 2015-07-24 10:39:49.477 Mvp_Discern[1125:19054] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因: '-[__NSCFString count]: 无法识别的选择器发送到实例 0x7966c8b0'
标签: ios objective-c json uitableview