从子类别中检索特定的JSON密钥

时间:2015-02-02 19:19:53

标签: ios json nsarray nsdictionary

我只在服务器上检索一次数据作为我的TableView上的JSON,它运行正常。我无法从JSON打印特定主题。

例如,根据我的JSON,我只想打印Dining RoomCoffees没有Bedroom的名称。我怎么能做到这一点?

我只能打印子类别的全名>产品(名称)和我的代码。

代码:

-(void)getJSON{

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;

    for (NSDictionary *dict in [_dic objectForKey:@"categories"]) {

        if ([[dict valueForKey:@"sub_category"]  isKindOfClass:[NSArray class]])
        {
            [_arraySubCategory addObject:[[[dict valueForKey:@"sub_category"] valueForKey:@"products"] valueForKey:@"name"]];

        }

    }

我的JSON数据:

Array
(
[categories] => Array
    (
        [0] => Array
            (
                [category_id] => 100
                [name] => Shop By Room
                [sub_category] => Array
                    (
                        [0] => Array
                            (
                                [category_id] => 72
                                [name] => BEDROOM
                                [sub_category] => 0
                                [product_total] => 11
                                [products] => Array
                                    (
                                        [0] => Array
                                            (
                                                [product_id] => 138
                                                [name] => Jewellery  Holder
                                            )

                                        [1] => Array
                                            (
                                                [product_id] => 139
                                                [name] => Jewellery Holder
                                            )

                                            )

                                    )

                            )

                        [1] => Array
                            (
                                [category_id] => 67
                                [name] => DINING ROOM
                                [sub_category] => 0
                                [product_total] => 73
                                [products] => Array
                                    (
                                        [0] => Array
                                            (
                                                [product_id] => 248
                                                [name] => Amuse
                                            )

                                        [1] => Array
                                            (
                                                [product_id] => 239
                                                [name] => Amuse
                                            )

                                            )
                                            .
                                            .
                                            .
       [1] => Array
            (
                [category_id] => 101
                [name] => Drink
                [sub_category] => Array
                    (
                        [0] => Array
                            (
                                [category_id] => 103
                                [name] => Coffees 
                                [sub_category] => 0
                                [product_total] => 14
                                [products] => Array
                                    (
                                        [0] => Array
                                            (
                                                [product_id] => 229
                                                [name] => Cafe1
                                            )

                                        [1] => Array
                                            (
                                                [product_id] => 233
                                                [name] => Cafe2

                                            )

1 个答案:

答案 0 :(得分:1)

我真的不确定你在问什么。看起来您只想打印Coffees内的值。只需使用if (...) continue;跳过与您想要的条件不符的类别和子类别。

for (NSDictionary *dict in [_dic objectForKey:@"categories"]) {

    if (![dict[@"name"] isEqualToString:@"Drink"]) continue;

    for (NSDictionary *subcategory in [dict valueForKey:@"sub_category"])
    {

        if (![subcategory[@"name"] isEqualToString:@"Coffees"]) continue;

        for (NSDictionary *product in subcategory[@"products"]) {
            [_arraySubCategory addObject:product[@"name"]];
        }

    }

}

您还应该知道,您发布的JSON是有效的JSON。这似乎是来自PHP的print_r