Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取JSON子类别元素问题_Ios_Nsarray_Nsdictionary - Fatal编程技术网

Ios 获取JSON子类别元素问题

Ios 获取JSON子类别元素问题,ios,nsarray,nsdictionary,Ios,Nsarray,Nsdictionary,我正在重新修改JSON数据,我有一些类别名称带有sub_category,另一些没有=0。我可以获取类别名称,它工作正常,但我无法获取子类别中的名称,当我调用\u arraySubCategory时,它会崩溃错误:原因:'-[\u NSCFArray objectForKey::]:无法识别的选择器发送到实例 我调用的方法正确吗?我的问题在哪里 - (IBAction)getData:(id)sender { NSString *string = BaseURLString; NSURL *u

我正在重新修改JSON数据,我有一些类别名称带有sub_category,另一些没有
=0
。我可以获取类别名称,它工作正常,但我无法获取
子类别中的
名称
,当我调用
\u arraySubCategory
时,它会崩溃错误:
原因:'-[\u NSCFArray objectForKey::]:无法识别的选择器发送到实例

我调用的方法正确吗?我的问题在哪里

- (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,它将是一个
[
字符。您需要索引数组,而不是通过键访问它


这意味着无论变量如何声明,
\u subcategory
都不是字典。变量的类型(大部分)是不相关的——重要的是变量地址的对象类型,这是一个数组。它是一个数组,因为“sub\u category”的值是一个数组(如
字符)

如果你有一个标有“狗”的盒子,并把一只猫放在里面,猫不会变成狗

此类不符合密钥名称的键值编码。

这与“unrecognized selector”的错误基本相同,只是不同。“sub_category”的零值作为NSNumber传递,您必须测试从获取“sub_category”返回的值的类型,以查看它是NSNumber还是NSArray。您可以使用测试类型,例如:

if ([theSubCategoryValue isKindOfClass:[NSNumber class]])

标记NSArray的开始。如果您查看原始JSON,它将是一个
[
字符。您需要索引数组,而不是通过键访问它。可能重复@HotLicks我很抱歉我弄糊涂了。现在我已将
\u dic子类别
更改为
\u arraySubCategory
(数组)。请您解释一下好吗?
\u subcategory
不是字典。变量的类型(大部分)是不相关的——重要的是变量地址的对象类型,这是一个数组。它是一个数组,因为“sub\u category”的值是一个数组(如
字符)所示).如果你有一个标有“狗”的盒子,并把一只猫放在里面,猫不会变成狗。