ios - Fetching recursive NSDictionaries and NSArray form JSON -
i new xcode , trouble find solution fetch nsarray , nsdictionaries json. please me. here json:
[ { "categoryid": "1", "category_parentid": "0", "category_name_english": "telivision", "category_name_arabic": "واعتلاء ثم. حاول المضي عن أخر", "category_date_edited": "2013-02-16 06:55:02", "sub": [ { "categoryid": "2", "category_parentid": "1", "category_name_english": "sharp", "category_name_arabic": "واعتلاء ثم. حاول المضي عن أخر", "category_date_edited": "2013-02-16 06:55:02", "sub": [] }, { "categoryid": "5", "category_parentid": "1", "category_name_english": "lg", "category_name_arabic": "", "category_date_edited": "2014-05-22 00:00:00", "sub": [ { "categoryid": "7", "category_parentid": "5", "category_name_english": "lg 1", "category_name_arabic": "", "category_date_edited": "2014-05-22 00:00:00", "sub": [] }, { "categoryid": "8", "category_parentid": "5", "category_name_english": "lg 2", "category_name_arabic": "", "category_date_edited": "2014-05-17 00:00:00", "sub": [] } ] } ] }, { "categoryid": "3", "category_parentid": "0", "category_name_english": "computer", "category_name_arabic": "واعتلاء ثم. حاول المضي عن أخر", "category_date_edited": "2013-02-16 06:55:02", "sub": [ { "categoryid": "4", "category_parentid": "3", "category_name_english": "hp colour", "category_name_arabic": "واعتلاء ثم. حاول المضي عن أخر", "category_date_edited": "2013-02-16 06:55:02", "sub": [] }, { "categoryid": "6", "category_parentid": "3", "category_name_english": "dell", "category_name_arabic": "", "category_date_edited": "2014-05-16 00:00:00", "sub": [ { "categoryid": "10", "category_parentid": "6", "category_name_english": "i5", "category_name_arabic": "", "category_date_edited": "2014-05-09 00:00:00", "sub": [] }, { "categoryid": "11", "category_parentid": "6", "category_name_english": "i7", "category_name_arabic": "", "category_date_edited": "2014-05-23 00:00:00", "sub": [] } ] } ] }, { "categoryid": "12", "category_parentid": "0", "category_name_english": "mobile", "category_name_arabic": "", "category_date_edited": "2014-05-15 00:00:00", "sub": [] } ]
my code here:
- (void)viewdidload { [super viewdidload]; radataobject *category; (nsdictionary *dict in jsonarray) { nsarray *subarray = [dict valueforkey:@"sub"]; if(subarray.count ==0) { category = [radataobject dataobjectwithname:[dict valueforkey:category_name_english] children:nil]; } else { category = [radataobject dataobjectwithname:[dict valueforkey:category_name_english] children:[self getsubcategories:subarray]]; } [mutableaddarray addobject:category]; } self.data = [mutableaddarray copy]; } -(nsarray *)getsubcategories:(nsarray *)subarray { nsmutablearray *duplicate = [[nsmutablearray alloc]init]; (nsdictionary *dct in subarray) { [duplicate addobject:[dct valueforkey:category_name_key]]; } nsarray *returnarray = [duplicate copy]; return returnarray; }
is possible using recursion?
this code might work your.. not checked..
-(void)getsubcategories:(nsarray *)subarray { (nsdictionary *mydict in subarray) { nsstring *first = [mydict objectforkey : @"category_name_english"]; nsarray *myubarray = [mydict objectforkey : @"sub"]; if([mysubarray count]) [self getsubcategories:mysubarray]; } }
Comments
Post a Comment