ios - client server json response -
i need display particular object key(currency) using post method after getting response web.
#import "viewcontroller.h" @interface viewcontroller () @end @implementation viewcontroller{ nsmutabledata *mutabledata; nsmutablestring *arr; #define url @"website" // change url #define no_connection @"no connection" #define no_values @"please enter parameter values" } - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } -(ibaction)senddatausingpost:(id)sender{ [self senddatatoserver :@"post"]; } -(ibaction)senddatausingget:(id)sender{ [self senddatatoserver : @"get"]; } -(void) senddatatoserver : (nsstring *) method{ nsstring *branchid=@"3"; serverresponse.text = @"getting response server..."; nsurl *url = nil; nsmutableurlrequest *request = nil; if([method isequaltostring:@"get"]){ nsstring *geturl = [nsstring stringwithformat:@"%@?branch_id=%@", url, branchid]; url = [nsurl urlwithstring: geturl]; request = [nsmutableurlrequest requestwithurl:url]; nslog(@"%@",geturl); }else{ // post nsstring *parameter = [nsstring stringwithformat:@"branch_id=%@",branchid]; nsdata *parameterdata = [parameter datausingencoding:nsutf8stringencoding allowlossyconversion:yes]; url = [nsurl urlwithstring: url]; nslog(@"%@", parameterdata); request = [nsmutableurlrequest requestwithurl:url]; [request sethttpbody:parameterdata]; arr= [nsmutablestring stringwithutf8string:[parameterdata bytes]]; nslog(@"responsedata: %@", arr); //nslog(@"%@",[[arr valueforkey:@"branchbylist"]objectforkey:@"currency"]); } [request sethttpmethod:method]; [request addvalue: @"application/x-www-form-urlencoded; charset=utf-8" forhttpheaderfield:@"content-type"]; nsurlconnection *connection = [[nsurlconnection alloc] initwithrequest:request delegate:self]; //nslog(@"%@",[connection valueforkeypath:@"branchbylist.currency"]); if( connection ) { mutabledata = [nsmutabledata new]; //nslog(@"%@",[connection valueforkeypath:@"branchbylist.currency"]); } } -(void) connection:(nsurlconnection *) connection didreceiveresponse:(nsurlresponse *)response { [mutabledata setlength:0]; } -(void) connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [mutabledata appenddata:data]; } -(void) connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { serverresponse.text = no_connection; return; } -(void)connectiondidfinishloading:(nsurlconnection *)connection { nsmutablestring *responsestringwithencoded = [[nsmutablestring alloc] initwithdata: mutabledata encoding:nsutf8stringencoding]; //nslog(@"response server : %@", responsestringwithencoded); nslog(@"%@",responsestringwithencoded ); nslog(@"%@",[responsestringwithencoded valueforkeypath:@"branchbylist.currency"] ); nsattributedstring * attrstr = [[nsattributedstring alloc] initwithdata:[responsestringwithencoded datausingencoding:nsunicodestringencoding] options:@{ nsdocumenttypedocumentattribute: nshtmltextdocumenttype } documentattributes:nil error:nil]; serverresponse.attributedtext = attrstr; // nslog(@"%@",attrstr); } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. }
i got response branch_id=3 want show "currency" tried lot failure.
my response need display currency..... response server :
{"branchbylist": [ {"id":"342","flag_image":"http:\/\/demo.techzarinfo.com\/newantara\/images\/flags\/usd.png","units":"1","code":"usd b","currency":"us dollar big","buy":"4.36","sell":"4.395","updated":"2016-04-11 03:24:24" }, {"id":"342","flag_image":"http:\/\/demo.techzarinfo.com\/newantara\/images\/flags\/usd.png","units":"1","code":"usd b","currency":"us dollar big","buy":"4.36","sell":"4.395","updated":"2016-04-11 03:24:24" } ]};
your response structure is:
-dictionary --array ---dictionary objects
you need convert data nsdictionary
parse it.
following code you:
nserror* error; nsdictionary* json = [nsjsonserialization jsonobjectwithdata: mutabledata options:kniloptions error:&error]; //now got top level dictionary nsarray* responsearray = [json objectforkey:@"branchbylist"]; //now got mid level response array //get embeded objects response array: nsdictionary *pricedic = [responsearray objectatindex:0]; //getting first object since arent telling second object nsstring *buyingprice = [pricedic objectforkey: @"buy"]; //buying price nsstring *sellingprice = [pricedic objectforkey:@"sell"]; //selling price nsstring *currency = [pricedic objectforkey:@"currency"]; //currency
though sticking point , getting job done.
proper way job done create model class response. create class inherited nsobject
, use model response. add initwithdic:
method class, pass response dic parameter , delegate dictionary parsing method.
also, nsurlconnection
deprecated since ios 9.0. should use nsurlsession
instead.
Comments
Post a Comment