ios - Parsing json from array of array -
i have json
data within first array main category array name "response" within have parent category array name "0" within have child category array name "0" , here 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 shelf","menuid":"4","0":[{"catname":"testing 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 want display in expandable tableview below
jewellery rings engagement rings wedding rings kk pendants ofice wear collections sound of love love bracelet colorful affair passion ring
here code 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; (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; (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]; } }
i tried coding you.follow code , customize want add array , set dictionary.
nsdictionary *jsonarray = [nsjsonserialization jsonobjectwithdata:responsedata options: nsjsonreadingmutablecontainers error: &err]; nsarray *array=[jsonarray objectforkey:@"response"]; (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); (int j=0; j<[arraydictvalue count]; i++) { nsmutabledictionary *dictinside = [arraydictvalue objectatindex:j]; nsarray *arrayinsidedict = [dictinside valueforkey:@"0"]; (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 - %@",strscatname); nslog(@"the strscatid - %@",strscatid); } } }
Comments
Post a Comment