0

I have the json data were within the first array I main category array with name "Response" within that I have parent category array with name "0" within which I have child category array with name "0" and here is my json format

{"Response":[{"menuname":"Jewellery","menuid":"1","0":[{"catname":"Rings","catid":"1","0":[{"scatname":"Engagement Rings","scatid":"4"},{"scatname":"Wedding Rings","scatid":"5"},{"scatname":"kk","scatid":"35"}]},{"catname":"Pendants","catid":"2","0":[{"scatname":"Office Wear","scatid":"8"}]},{"catname":"Bracelets","catid":"3","0":[{"scatname":"Studded","scatid":"9"}]},{"catname":"Earrings","catid":"6","0":[{"scatname":"Ethnic Jhumkas","scatid":"7"}]},{"catname":"Chain's","catid":"33","0":[]},{"catname":"Jewel","catid":"34","0":[]}]},{"menuname":"Collections","menuid":"2","0":[{"catname":"SOUND OF LOVE","catid":"15","0":[{"scatname":"LOVE BRACELET","scatid":"16"}]},{"catname":"COLORFUL AFFAIR","catid":"17","0":[{"scatname":"Passion ring","scatid":"18"}]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]},{"menuname":"Gold Coin","menuid":"3","0":[{"catname":"SOUND OF LOVE","catid":"15","0":[{"scatname":"LOVE BRACELET","scatid":"16"}]},{"catname":"COLORFUL AFFAIR","catid":"17","0":[{"scatname":"Passion ring","scatid":"18"}]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]},{"menuname":"OFF THE SHELF","menuid":"4","0":[{"catname":"testing from pixel","catid":"13","0":[]},{"catname":"New pixel","catid":"23","0":[]},{"catname":"Evermore Collection","catid":"19","0":[]},{"catname":"BOARDROOM GLAM ","catid":"20","0":[]},{"catname":"ETERNAL GOLD","catid":"21","0":[]},{"catname":"FASHIONISTA COLLECTION","catid":"22","0":[]}]}]}

and I want to display in expandable tableview as below

Jewellery
  Rings
     Engagement Rings
     Wedding Rings
     kk
  Pendants
     Ofice Wear
Collections
  Sound Of  Love
     Love bracelet
  Colorful Affair
     Passion ring

Here is the code I used in viewdidLoad

 NSDictionary *pJson;
    NSMutableString *postStr = [NSMutableString stringWithString:kURL];
  [postStr appendString:[NSString stringWithFormat:@"?tag=%@&id=%@",kCategoryFilter,kPrecious]];

    [postStr setString:[postStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:postStr]];
    NSLog(@"%@",postStr);
    [request setHTTPMethod:@"POST"];
    _connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
    NSURL *url = [NSURL URLWithString:postStr];
    NSData *data = [NSData dataWithContentsOfURL:url];
    pJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"%@",pJson);
    NSMutableArray *arr = [pJson objectForKey:@"Response"];
    NSLog(@"%lu",(unsigned long)arr.count);
    NSMutableDictionary *dict2 =[[NSMutableDictionary alloc]init];// [arr objectAtIndex:NSIndexPath.row];
   [dict2 setObject:arr forKey:@"dictionary1"];
   dict2 = [arr objectAtIndex:0];
   NSArray *arr1 =[dict2 objectForKey:@"0"];
    NSLog(@"%lu",(unsigned long)arr1.count);
    NSUInteger y;
    for (int i=0;i<arr.count;i++) {
              NSString *ring_data = [[arr objectAtIndex:i]objectForKey:@"menuname"];
        NSString *id_data = [[arr objectAtIndex:i]objectForKey:@"menuid"];
        NSLog(@"AUTHOR: %@",ring_data);
        NSLog(@"%@",id_data);
        dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                      ring_data,@"menuname",id_data,@"menuId",nil];
        [myObject addObject:dictionary];
        NSMutableArray *arr1 = [dict2 objectForKey:@"0"];
        NSLog(@"%lu",(unsigned long)arr1.count);
        y = arr.count;
        for (NSUInteger i=0;i<arr1.count;i++) {
            NSArray *count = [[[arr1 objectAtIndex:i] objectForKey:arr]valueForKey:@"catname"];
            NSString *myCount = [NSString stringWithFormat:@"%lu",(unsigned long)[count count]];
            NSString *ring_data = [[arr1 objectAtIndex:i]objectForKey:@"catname"];
            NSLog(@"AUTHOR: %@",ring_data);
            //  NSLog(@"%@",catname_data);
            dictionary1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          ring_data,@"catname",
                          nil];
            [myObject1 addObject:dictionary1];

        }      
}
iBhavin
  • 1,261
  • 15
  • 30

1 Answers1

0

I tried some coding for you.Follow that code and customize where you want to add to array and where to set dictionary.

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

NSArray *array=[jsonArray objectForKey:@"Response"];

for (int i=0;i<[array count];i++)
{
    NSMutableDictionary *dict = [array objectAtIndex:i];
    NSMutableArray *arrayDictValue = [dict valueForKey:[NSString stringWithFormat:@"%d",0]];
    NSString *strMenuName = [NSString stringWithFormat:@"%@",[arrayDictValue valueForKey:@"menuname"]];
    NSString *strMenuID = [NSString stringWithFormat:@"%@",[arrayDictValue valueForKey:@"menuid"]];
    NSLog(@"The strMenuName is-%@",strMenuName);
    NSLog(@"The strMenuID is-%@",strMenuID);
    for (int j=0; j<[arrayDictValue count]; i++)
    {
        NSMutableDictionary *dictInside = [arrayDictValue objectAtIndex:j];
        NSArray *arrayInsideDict = [dictInside valueForKey:@"0"];
        for (int k =0; k<[arrayInsideDict count]; i++)
        {

            NSString *strCatName = [NSString stringWithFormat:@"%@",[[arrayInsideDict objectAtIndex:k ]valueForKey:@"catname"]];
            NSString *strCatID = [NSString stringWithFormat:@"%@",[[arrayInsideDict objectAtIndex:k ]valueForKey:@"catid"]];
            NSLog(@"The strCateName is-%@",strCatName);
            NSLog(@"The strCatID is-%@",strCatID);
            NSMutableDictionary *dictInArray = [[arrayInsideDict objectAtIndex:0] valueForKey:@"0"];
            NSString *strSCatName = [NSString stringWithFormat:@"%@",[dictInArray valueForKey:@"scatname"]];
            NSString *strSCatID = [NSString stringWithFormat:@"%@",[dictInArray valueForKey:@"scatid"]];
            NSLog(@"the strSCatName is - %@",strSCatName);
            NSLog(@"the strSCatID is - %@",strSCatID);

        }


    }


}
user3182143
  • 9,459
  • 3
  • 32
  • 39