Server-side PHP file connect.php:
<?PHP$q=mysql_connect("localhost", "root", "" "); if(!$q) { die(' Could not connect: '.Mysql_error()); } mysql_query("Set names UTF8");//reading data with UTF8 mysql_select_db("Jasontest",$q);//Database $sql= "SELECT * from UserInfo"; $query=mysql_query($sql); while($row=Mysql_fetch_array($query)){ $com=$row; } EchoJson_encode ($com);?>
iOS segment parsing:
-(Ibaction) Clickbutton: (IDSender {[Nsthread detachnewthreadselector: @selector (getjasonstring) totarget:self Withobject:nil];}-(void) getjasonstring{Nserror*error; //loading a Nsurl objectNsurlrequest *request2 = [Nsurlrequest requestwithurl:[nsurl urlwithstring:@"http://localhost/jasontest/connect.php"]]; //put the requested URL data into the NSData objectNSData *response =[nsurlconnection sendsynchronousrequest:request2 returningresponse:nil Error:nil]; //IOS5 self-contained parsing class Nsjsonserialization parse data from response into a dictionaryNsdictionary *jasondic = [nsjsonserialization jsonobjectwithdata:response options:nsjsonreadingmutableleaves Error: &ERROR]; NSLog (@"Person ' s userid is%@", [Jasondic Objectforkey:@"userid"]); NSLog (@"Person ' s name is%@", [Jasondic Objectforkey:@"name"]); NSLog (@"Person ' s password is%@", [Jasondic Objectforkey:@"Password"]); NSLog (@"Person ' s Phoneno is%@", [Jasondic Objectforkey:@"Phoneno"]); }
[ios+php Jason format for sending and parsing]