2015-08-08 3 views
-1

Это ответ со стороны сервера, которую я получаю. соответствует словарю.
Я использую значение для ключа, но не помогая мне.UITableview с NSArray содержит NSDictionary и NSDictionary containg другой 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]]; 
} 
+0

делает любой знает, как напечатать это значение – user3189586

+0

ваш JSON формиат трудно потому что ваш в дополнительном ключе данных, например 0 =, 1 = что-то это удалить из json не нужно. @ user3189586 –

+0

есть что-то есть, чтобы удалить те 0 =, 1 = – user3189586

ответ

0

Попробуйте этот код, чтобы получить отдачу от 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 

    } 

использовать этот способ в методе делегата таблицы

- (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; 
} 
+0

Вы потрясающий. Это работает для m e ..... спасибо u. – user3189586

+0

добро пожаловать в любое время @ user3189586 –

1

Поскольку у вас есть нерегулярные ключи, попробуйте это, я попробовал и использовал 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); 
} 
0

если кто сталкивается с этой же проблемой

-(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; 
    } 
0

Handling ответ:

- (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; 
} 
Смежные вопросы