ios - How to differanitate data from json when the data is in two dictionaries? -
i fetching data json. gives response in 2 dictionaries. how differentiate dictionaries , paste data in table view. using segment control in table view. 1 receiver , other sender.
nsuserdefaults *uidsave = [nsuserdefaults standarduserdefaults]; nsmutabledictionary *get = [[nsmutabledictionary alloc]init]; [get setobject:[uidsave valueforkey:@"uid"]forkey:@"uid"]; nslog(@"dictionary data %@",get); nsdata* jsondata = [nsjsonserialization datawithjsonobject:get options:kniloptions error:nil]; nsstring *jsoninputstring = [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding]; nsstring *post = [[nsstring alloc]initwithformat:@"r=%@",jsoninputstring]; nsurl *url=[nsurl urlwithstring:[nsstring stringwithformat:@"%@",caseinfourl]]; nsdata *postdata = [post datausingencoding:nsasciistringencoding allowlossyconversion:yes]; nsstring *postlength = [nsstring stringwithformat:@"%lu", (unsigned long)[postdata length]]; nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestreloadignoringcachedata timeoutinterval:120.0]; [request seturl:url]; [request sethttpmethod:@"post"]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request sethttpbody:postdata]; nserror *error; nsurlresponse *response; nsdata *responsedata=[nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; if (responsedata != nil) { jsonarray = (nsmutabledictionary *)[nsjsonserialization jsonobjectwithdata:responsedata options:kniloptions error:&error]; nslog(@"values =======%@",jsonarray); sender = [jsonarray objectforkey:@"sender"]; nslog(@"sender^^^^%@",sender); receiver = [jsonarray objectforkey:@"reciever"]; nslog(@"reciever$$$$%@",receiver); } if (error) { nslog(@"error %@",error.description); uialertview *alertview = [[uialertview alloc]initwithtitle:@"error" message:@"server not responding" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:nil, nil]; [alertview show]; } // set names array sendarray = [[nsmutablearray alloc]init]; // loop through our json array (int = 0 ; <sender.count; i++) { //create object nsstring *datetime = [[sender objectatindex:i]objectforkey:@"datetime"]; nsstring *pronumber = [[jsonarray objectatindex:i]objectforkey:@"pro_number"]; nsstring *statuses = [[jsonarray objectatindex:i]objectforkey:@"status"]; [sendarray addobject:[[dataobjects alloc]initwithdate1:datetime andcasename1:pronumber andstatus1:statuses]]; } [sendlist reloaddata]; }
define bool variable
bool sendersectionisselected; if want load sender's data in viewdidload:-
sendersectionisselected=yes; if want load receiver's data set no;
sendersectionisselected=no; modify this
if (responsedata != nil) { jsonarray = (nsmutabledictionary *)[nsjsonserialization jsonobjectwithdata:responsedata options:kniloptions error:&error]; nslog(@"values =======%@",jsonarray); sender = [jsonarray objectforkey:@"sender"]; nslog(@"sender^^^^%@",sender); receiver = [jsonarray objectforkey:@"reciever"]; nslog(@"reciever$$$$%@",receiver); [yourtableview reloaddata]; } in numberofrow , cellforrow load data accordingly:-
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (sendersectionisselected) { return sender.count; } else { return receiver.count; } } in cellforrow show data accordingly:-
if (sendersectionisselected) { yourdatatimelabel.text=[[sender objectatinder:indexpath.row]objectforkey:@"datetime"]; yourpronumberlabel.text=[[sender objectatinder:indexpath.row]objectforkey:@"pro_number"]; yourstatuslabel.text=[[sender objectatinder:indexpath.row]objectforkey:@"status"]; } else { yourdatatimelabel.text=[[receiver objectatinder:indexpath.row]objectforkey:@"datetime"]; yourpronumberlabel.text=[[receiver objectatinder:indexpath.row]objectforkey:@"pro_number"]; yourstatuslabel.text=[[receiver objectatinder:indexpath.row]objectforkey:@"status"]; } now in segmentcontroller's action methods:-
if segment sender then:-
sendersectionisselected=yes; if segment receiver then:-
sendersectionisselected=no; //don't forget reload tableview sendersectionisselected=yes;
Comments
Post a Comment