获取 JSON 子类别元素问题
Getting JSON Sub Category Element Issue
我正在检索 JSON 数据,我有一些类别名称带有 sub_category,而其他类别名称没有 =0
。我可以获取类别名称并且它工作正常但是我无法在 sub_category
中获取 name
,当我调用 _arraySubCategory
时它正在崩溃错误:reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance
我调用的方法正确吗?我的问题在哪里?
- (IBAction)getData:(id)sender {
NSString *string = BaseURLString;
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
_dic = (NSDictionary *)responseObject;
NSLog(@"Data %@", _dic);
for (NSDictionary *dict in [_dic objectForKey:@"categories"]) {
[_arrayName addObject:[dict objectForKey:@"name"]];
_dicSubCategory = [dict objectForKey:@"sub_category"];
[_arraySubCategory addObject:[_dicSubCategory objectForKey:@"name"]]; //Crashing
NSLog(@"%@", _dicSubCategory);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog("No Connection")
}];
[operation start];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_arrayName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", [_arrayName objectAtIndex:indexPath.row]];
return cell;
}
JSON数据:
{
categories = (
{
name = "Shop By Room";
"sub_category" = (
{
name = "BEDROOM";
"sub_category" = 0;
},
{
name = "LIVING ROOM";
"sub_category" = 0;
}
);
},
{
name = "Drinkware";
"sub_category" = (
{
name = "Glass1";
"sub_category" = 0;
},
{
name = "Glass2";
"sub_category" = 0;
},
{
name = "Service";
"sub_category" = 0;
},
{
name = "Design";
"sub_category" = (
{
name = "RA";
"sub_category" = 0;
},
{
name = "SW";
"sub_category" = 0;
}
);
},
{
name = "Collections";
"sub_category" = 0;
);
}
);
}
(
标记 NSArray 的开始。如果您查看原始 JSON,那将是一个 [
字符。您需要索引一个数组,而不是通过键访问它。
这意味着 _dicSubCategory
不是字典,无论变量是如何声明的。变量的类型(大部分)是无关紧要的——重要的是变量地址的对象类型,即数组。它是一个数组,因为 "sub_category" 的值是一个数组(从 (
字符可以看出)。
如果你有一个标有 "dog" 的盒子并在里面放了一只猫,猫就不会变成狗。
this class is not key value coding-compliant for the key name.
这与 "unrecognized selector" 本质上是相同的错误,只是有所不同。 "sub_category" 的零值作为 NSNumber,您必须测试从获取 "sub_category" 返回的值的类型,以查看它是 NSNumber 还是 NSArray。您可以测试类型,例如:
if ([theSubCategoryValue isKindOfClass:[NSNumber class]])
我正在检索 JSON 数据,我有一些类别名称带有 sub_category,而其他类别名称没有 =0
。我可以获取类别名称并且它工作正常但是我无法在 sub_category
中获取 name
,当我调用 _arraySubCategory
时它正在崩溃错误:reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance
我调用的方法正确吗?我的问题在哪里?
- (IBAction)getData:(id)sender {
NSString *string = BaseURLString;
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
_dic = (NSDictionary *)responseObject;
NSLog(@"Data %@", _dic);
for (NSDictionary *dict in [_dic objectForKey:@"categories"]) {
[_arrayName addObject:[dict objectForKey:@"name"]];
_dicSubCategory = [dict objectForKey:@"sub_category"];
[_arraySubCategory addObject:[_dicSubCategory objectForKey:@"name"]]; //Crashing
NSLog(@"%@", _dicSubCategory);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog("No Connection")
}];
[operation start];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_arrayName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@", [_arrayName objectAtIndex:indexPath.row]];
return cell;
}
JSON数据:
{
categories = (
{
name = "Shop By Room";
"sub_category" = (
{
name = "BEDROOM";
"sub_category" = 0;
},
{
name = "LIVING ROOM";
"sub_category" = 0;
}
);
},
{
name = "Drinkware";
"sub_category" = (
{
name = "Glass1";
"sub_category" = 0;
},
{
name = "Glass2";
"sub_category" = 0;
},
{
name = "Service";
"sub_category" = 0;
},
{
name = "Design";
"sub_category" = (
{
name = "RA";
"sub_category" = 0;
},
{
name = "SW";
"sub_category" = 0;
}
);
},
{
name = "Collections";
"sub_category" = 0;
);
}
);
}
(
标记 NSArray 的开始。如果您查看原始 JSON,那将是一个 [
字符。您需要索引一个数组,而不是通过键访问它。
这意味着 _dicSubCategory
不是字典,无论变量是如何声明的。变量的类型(大部分)是无关紧要的——重要的是变量地址的对象类型,即数组。它是一个数组,因为 "sub_category" 的值是一个数组(从 (
字符可以看出)。
如果你有一个标有 "dog" 的盒子并在里面放了一只猫,猫就不会变成狗。
this class is not key value coding-compliant for the key name.
这与 "unrecognized selector" 本质上是相同的错误,只是有所不同。 "sub_category" 的零值作为 NSNumber,您必须测试从获取 "sub_category" 返回的值的类型,以查看它是 NSNumber 还是 NSArray。您可以测试类型,例如:
if ([theSubCategoryValue isKindOfClass:[NSNumber class]])