asp.net - Cant get Values from JSON Webservice -
i have webservice, picks data database ans send them in json iphone
my webservice:
[scriptmethod(responseformat = responseformat.json)] [webmethod] public string person() { return helper.person(); } my helper.cs
public class helper { internal static string person() { list<object> person = new list<object>(); using (sqlconnection con = new sqlconnection(@"data source=localhost\sqlexpress;initial catalog=book-it-v2;integrated security=true;")) using (sqlcommand cmd = new sqlcommand(@"select email person", con)) { con.open(); using (sqldatareader rdr = cmd.executereader()) { while (rdr.read()) { if (rdr["email"] != dbnull.value) { raumklassenobject.add(new { email=rdr["email"].tostring()}); } } } } return new javascriptserializer().serialize(person.toarray()); } } in xcode:
#import "raumklasseviewcontroller.h" @interface raumklasseviewcontroller () @end @implementation raumklasseviewcontroller @synthesize result; @synthesize dic; @synthesize array; - (id)initwithstyle:(uitableviewstyle)style { self = [super initwithstyle:style]; if (self) { // custom initialization } return self; } - (void) sendrequest:(nsstring*)jsontext { nsurl *url = [[nsurl alloc]initwithstring:@"http://ndqqxtsdsdoludwons-entwickludng.de/webdiendst22/service1.asmx/raumklasse"]; nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; request = [nsmutableurlrequest requestwithurl:url cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:10]; [request sethttpmethod: @"post"]; [request setvalue:@"application/json" forhttpheaderfield:@"content-type"]; nsdata *reqdata = [nsdata datawithbytes:[jsontext utf8string] length:[jsontext length]]; [request sethttpbody:reqdata]; nsurlresponse *response =[[nsurlresponse alloc]init]; nserror* error; result = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; } - (void)viewdidload { [super viewdidload]; [self sendrequest:@""]; dic = [nsjsonserialization jsonobjectwithdata:result options:kniloptions error:nil]; self.array= [dic objectforkey:@"d"]; nsmutablearray *finalarray = [[nsmutablearray alloc] init]; for(int n=0; n<[self.array count]; n++) [finalarray addobject:[[self.array objectatindex:n] objectforkey:@"email"]]; } - (void)viewdidunload { [super viewdidunload]; } - (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { return (interfaceorientation == uiinterfaceorientationportrait); } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // return number of sections. return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [self.array count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } cell.textlabel.text=[array objectatindex:indexpath.row]; return cell; } @end so far works , output is:
[{"email":"abcd@web.de"},{"email":"acsb@web.de"},{"email":"asbd@gmx.de"},{"email":"abcdw@web.de"},{"email":"absds@web.de"}] but if try
self.array = [[dic objectforkey:@"d"] objectforkey:@"email"]; to values : abcd@web.de, acsb@web.de, asbd@gmx.de etc.
so iam getting error:
[__nscfstring objectforkey:]: unrecognized selector sent instance 0x6a4ccb0. i think webservice returning wrong. , dont know what. maybe can me.
thank in advance.
you have array of dictionaries 1 single key in each dictionary ("email").
objectforkey: method returns object key in dictionary.
you can not call method on array receiving [dic objectforkey:@"d"] because method not work on arrays.
instead use objectatindex: this:
self.array = [dic objectforkey:@"d"]; nsmutablearray *finalarray = [[nsmutablearray alloc] init]; for(int n=0; n<[self.array count]; n++) [finalarray addobject:[[self.array objectatindex:n] objectforkey:@"email"]]; once done running finalarray contain array of strings of email addresses only.
Comments
Post a Comment