带有 NSArray 的 UITableview 包含 NSDictionary 和包含另一个 NSDictionary 的 NSDictionary

UITableview with NSArray contains NSDictionary and NSDictionary containg another NSDictionary

这是我收到的来自服务器端的响应。 匹配是字典。
我正在使用键值但没有帮助我。

       {
    0 =     {

    "shop_name" = sujeet;
    subcategory =         {
        0 =             {
            subcategory = "Hair cut";
            "subcategory_id" = 16;
        };
    };
};
1 =     {

    "shop_name" = "Scissors Men's Parlour";
    subcategory =         {
        0 =             {
            subcategory = "Children Hair Cut";
            "subcategory_id" = 19;
        };
    };
};
2 =     {

    "shop_name" = "Mirror Beauty Parlour";
    subcategory =         {
        0 =             {
            subcategory = "Beauty parlour";
            "subcategory_id" = 14;
        };
        1 =             {
            subcategory = Manicure;
            "subcategory_id" = 25;
        };
        10 =             {
            subcategory = "Wax Half Legs";
            "subcategory_id" = 34;
        };
        11 =             {
            subcategory = "Eyebrow Tint";
            "subcategory_id" = 36;
        };
        2 =             {
            subcategory = Pedicure;
            "subcategory_id" = 26;
        };
        3 =             {
            subcategory = "Facial Massage";
            "subcategory_id" = 27;

        };
    };
};
3 =     {

    "shop_name" = "Sheela's beauty salon";
    subcategory =         {
        0 =             {
            subcategory = "Children Hair Cut";
            "subcategory_id" = 19;
        };

    };
};
};
}

我想将子类别和 shop_name 放在 uitableview 单元格上 我的代码 首先我想知道我得到了多少字典数

     myCount = [MAtches count];
           NSString *inStr;
            for (i = 0; i < myCount; i++)
            {
                inStr = [@(i) stringValue];
                [connt addObject:inStr];

            }

doing this 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
{
    cell.Shop_Name.text=[MAtches valueForKey:[connt objectAtIndex:indexPath.row]];
}

尝试使用此代码从您的 json 响应中获取价值

NSMutableDictionary *dict= yourdata store in dictionary;

    for (int i=0; i<[dict allKeys].count; i++) {
        NSLog(@"%@",[dict objectForKey:[NSString stringWithFormat:@"%d",i]]);// return first array object means 0 key value
        NSLog(@"%@",[[dict objectForKey:[NSString stringWithFormat:@"%d",i]] objectForKey:@"shop_name"]);// return shop name

    }

在您的 table 委托方法中使用这种方式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.Shop_Name.text=[[yourJsonDictionary objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]] objectForKey:@"shop_name"]; // print shop name 
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [yourJsonDictionary allKeys].count;
}

因为你有一个不规则的键试试这个,我尝试并使用 NSDictionary : 2 来制作这个例子..;)干杯。

NSDictionary *tempDic = @{
    @"shop_name" : @"Mirror Beauty Parlour",
    @"subcategory":
       @{
           @0: @{
                   @"subcategory": @"Beauty parlour",
                   @"subcategory_id": @14
                },
           @1: @{
                   @"subcategory": @"Manicure",
                   @"subcategory_id": @25
               },
           @10:@{
                   @"subcategory": @"Wax Half Legs",
                   @"subcategory_id": @35
               },
           @11:@{
                   @"subcategory": @"Eyebrow Tint",
                   @"subcategory_id": @36
               },
           @2: @{
                   @"subcategory": @"Pedicure",
                   @"subcategory_id": @26
               },
           @3: @{
                   @"subcategory": @"Facial Massage",
                   @"subcategory_id": @27
               }
           }
       };

NSLog(@"shop_name: %@", [tempDic objectForKey:@"shop_name"]);

NSDictionary *tempDict = [tempDic objectForKey:@"subcategory"];

NSArray *allKeys = [tempDict allKeys];

for (int i = 0; i < allKeys.count; i++)
{
    NSString *key = allKeys[i];

    NSDictionary *item = [tempDict objectForKey:key];

    NSLog(@"key:%@", key);

    NSLog(@"subcategory:%@", [item objectForKey:@"subcategory"]);

    NSLog(@"subcategory_id:%@", [item objectForKey:@"subcategory_id"]);

    NSLog(@"dictionary:%@", item);
}

如果有人遇到同样的问题

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
   NSArray *key=[MAtches valueForKey:[connt objectAtIndex:indexPath.row]];
    cell.Shop_Name.text=[key valueForKey:@"shop_name"];
     retrn cell;
    }

处理响应:

- (void)handleResponse:(NSDictionary *)response
{


responseParsedDictionary = [[NSMutableDictionary alloc] init];

    for (id key in response) {
        NSMutableDictionary *subDictionary = [[NSMutableDictionary alloc] init];
        [subDictionary setObject:[[response objectForKey:key] objectForKey:@"shop_name"] forKey:@"shop_name"];
        NSDictionary *subcategory = (NSDictionary *)[[response objectForKey:key] objectForKey:@"subcategory"];
        NSMutableDictionary *subCategoryArray = [[NSMutableDictionary alloc] init];
        for (id subKey in subcategory) {
            NSMutableDictionary *subcategoryDictionary = [[NSMutableDictionary alloc] init];
            [subcategoryDictionary setObject:[[subcategory objectForKey:subKey] objectForKey:@"subcategory"]forKey:@"subcategory"];
            [subcategoryDictionary setObject:[[subcategory objectForKey:subKey] objectForKey:@"subcategory_id"]forKey:@"subcategory_id"];
            [subCategoryArray setObject:subcategoryDictionary forKey:subKey];
        }
        [subDictionary setObject:subCategoryArray forKey:@"subcategory"];

        [responseParsedDictionary setObject:subDictionary forKey:key];

    }
}`enter code here`

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.Shop_Name.text= [[responseParsedDictionary objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] objectForKey:@"shop_name"];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return responseParsedDictionary.count;
}