Note that there are some explanatory texts on larger screens.

plurals
  1. POjson parse string
    text
    copied!<p>i create php server and i call this function in my objective c</p> <pre><code>public function getCOMPAINs (){ $query = "SELECT COMPAINCOMPAIN_ID FROM COMPAIN_USER WHERE USERUSER_ID = '$this-&gt;USER_ID'"; $MyConnection = new MyConnection(); $result = mysql_query($query,$MyConnection-&gt;Connection) or die(mysql_error()); while($obj=mysql_fetch_object($result)) { $res[] = array( 'COMPAIN_ID' =&gt; $obj-&gt;COMPAINCOMPAIN_ID); } return json_encode( $res); } </code></pre> <p>if i try call this function with php i can show this response</p> <pre><code>array(2) { [0]=&gt; array(1) { ["COMPAIN_ID"]=&gt; string(2) "44" } [1]=&gt; array(1) { ["COMPAIN_ID"]=&gt; string(2) "46" } } </code></pre> <p>How can I parse this response data with Json?i try to do this solution but this using with xml</p> <pre><code> - (NSMutableData *)sendRequestCompains{ NSMutableString *sRequest = [[NSMutableString alloc] init]; [sRequest appendString:@"&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;"]; [sRequest appendString:@"&lt;soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"&gt;"]; [sRequest appendString:@"&lt;soap:Body&gt;"]; [sRequest appendString:@"&lt;getCOMPAINs xmlns=\"http://tempuri.org/PlatformService/method\"&gt;"]; [sRequest appendString:@"&lt;Id_USR&gt;"]; // any attribute [sRequest appendString:@"1"]; // attribute value [sRequest appendString:@"&lt;/Id_USR&gt;"]; [sRequest appendString:@"&lt;/getCOMPAINs&gt;"]; [sRequest appendString:@"&lt;/soap:Body&gt;"]; [sRequest appendString:@"&lt;/soap:Envelope&gt;"]; NSURL *ServiceURL = [NSURL URLWithString:@"http://79.249.37.1/server_comp.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:ServiceURL]; [request addValue:@"text/xml; charset:UTF-8" forHTTPHeaderField:@"Content-Type"]; [request addValue:@"http://79.249.37.1/server_comp.php/getCOMPAINs" forHTTPHeaderField:@"SOAPAction"]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (conn) { data = [[NSData data] retain]; NSLog(@"Connection success"); [NSURLConnection connectionWithRequest:request delegate:self]; NSError *WSerror; NSURLResponse *WSresponse; data = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;WSresponse error:&amp;WSerror]; NSLog(@"slt%d",[data length]); } return data; } </code></pre> <p>i try this solution but not work</p> <pre><code>NSData * webData=[[NSMutableData alloc] initWithData:self.sendRequestCompains]; // Create new SBJSON parser object SBJSON *parser = [[SBJSON alloc] init]; NSString *json_string = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; NSArray *statuses = [parser objectWithString:json_string error:nil]; for (NSDictionary *status in statuses) { NSLog(@"%@", [status objectForKey:@"0"]); } the response recived from server &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;&lt;SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"&gt;&lt;SOAP-ENV:Body&gt;&lt;ns1:getCOMPAINsResponse xmlns:ns1="http://tempuri.org/PlatformService/method"&gt;&lt;COMPAIN xsi:type="xsd:string"&gt;[{&amp;quot;COMPAIN_ID&amp;quot;:&amp;quot;44&amp;quot;},{&amp;quot;COMPAIN_ID&amp;quot;:&amp;quot;46&amp;quot;}]&lt;/COMPAIN&gt;&lt;/ns1:getCOMPAINsResponse&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt; </code></pre> <p>i try this solution but not work</p> <blockquote> <pre><code>NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL </code></pre> <p>URLWithString:@"http://79.249.37.1/server_comp.php/getCOMPAINs.json?userId=1"]];</p> <pre><code> // execution de la requête et récupération du JSON via un objet </code></pre> <p>NSData NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];</p> <pre><code> // On récupère le JSON en NSString depuis la réponse NSString *json_string = [[NSString alloc] initWithData:response </code></pre> <p>encoding:NSUTF8StringEncoding];</p> <pre><code> // on parse la reponse JSON NSArray *statuses = [parser objectWithString:json_string </code></pre> <p>error:nil];</p> <pre><code> for (NSDictionary *status in statuses) { // on peut recuperer les valeurs en utilisant objectForKey à </code></pre> <p>partir du status qui est un NSDictionary // on log le tweet et le nom de l utilisateur NSLog(@"%@", [status objectForKey:@"COMPAIN_ID"]); }</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload