1

i create php server and i call this function in my objective c

public function getCOMPAINs (){

      $query = "SELECT COMPAINCOMPAIN_ID FROM COMPAIN_USER WHERE USERUSER_ID = '$this->USER_ID'";
            $MyConnection = new MyConnection();
      $result = mysql_query($query,$MyConnection->Connection) or die(mysql_error());
    while($obj=mysql_fetch_object($result)) {
        $res[] = array( 'COMPAIN_ID' => $obj->COMPAINCOMPAIN_ID);
    }
    return json_encode( $res);



  }

if i try call this function with php i can show this response

array(2) {
  [0]=>
  array(1) {
    ["COMPAIN_ID"]=>
    string(2) "44"
  }
  [1]=>
  array(1) {
    ["COMPAIN_ID"]=>
    string(2) "46"
  }
}

How can I parse this response data with Json?i try to do this solution but this using with xml

 - (NSMutableData *)sendRequestCompains{

        NSMutableString *sRequest = [[NSMutableString alloc] init];
        [sRequest appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
        [sRequest appendString:@"<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/\">"];
        [sRequest appendString:@"<soap:Body>"];
        [sRequest appendString:@"<getCOMPAINs xmlns=\"http://tempuri.org/PlatformService/method\">"];
        [sRequest appendString:@"<Id_USR>"];    // any attribute
        [sRequest appendString:@"1"];    // attribute value
        [sRequest appendString:@"</Id_USR>"];
        [sRequest appendString:@"</getCOMPAINs>"];
        [sRequest appendString:@"</soap:Body>"];
        [sRequest appendString:@"</soap:Envelope>"];


        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:&WSresponse error:&WSerror];
            NSLog(@"slt%d",[data length]);
        }






        return data;
    }

i try this solution but not work

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
<?xml version="1.0" encoding="ISO-8859-1"?><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/"><SOAP-ENV:Body><ns1:getCOMPAINsResponse xmlns:ns1="http://tempuri.org/PlatformService/method"><COMPAIN xsi:type="xsd:string">[{&quot;COMPAIN_ID&quot;:&quot;44&quot;},{&quot;COMPAIN_ID&quot;:&quot;46&quot;}]</COMPAIN></ns1:getCOMPAINsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

i try this solution but not work

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL

URLWithString:@"http://79.249.37.1/server_comp.php/getCOMPAINs.json?userId=1"]];

  // execution de la requête et récupération du JSON via un objet

NSData NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

  // On récupère le JSON en NSString depuis la réponse
  NSString *json_string = [[NSString alloc] initWithData:response

encoding:NSUTF8StringEncoding];

  // on parse la reponse JSON
  NSArray *statuses = [parser objectWithString:json_string

error:nil];

  for (NSDictionary *status in statuses)
  {
      // on peut recuperer les valeurs en utilisant objectForKey à

partir du status qui est un NSDictionary // on log le tweet et le nom de l utilisateur NSLog(@"%@", [status objectForKey:@"COMPAIN_ID"]); }

10
  • Add the response from your server (The JSON, not the PHP pritn_r output!) to the question please! One of your comments below says 'recive json in xml string' and that makes no sense at all - we need more information. Commented Jul 27, 2011 at 10:59
  • this the json includ in response [{&quot;COMPAIN_ID&quot;:&quot;44&quot;},{&quot;COMPAIN_ID&quot;:&quot;46&quot;}] Commented Jul 27, 2011 at 11:03
  • There's no XML there at all, why are you talking about 'xml string' ? Commented Jul 27, 2011 at 12:01
  • becuase the response recived start with <?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP- Commented Jul 27, 2011 at 12:04
  • so the code you posted in your previous comment wasn't the complete response! Why can't you parse the XML to get the JSON and then just parse the JSON? Commented Jul 27, 2011 at 12:18

3 Answers 3

3

You want to use JSON to parse the response but the response is wrapped in some XML.

You have two options :

1) Parse the XML to get the JSON and then parse that as JSON

2) Ask the server not to wrap your response in the XML, just return the JSON.

You seem to be talking to the server with SOAP (from sendRequestCompains) - there's not much chance that if you make a SOAP request it will return a JSON object! I think your best bet would be option (1).

However, if you have control over your server I would stop using SOAP at all and get it to response to something like

http://79.249.37.1/server_comp.php/getCOMPAINs.json?Id_USR=1

I'm passing the paramters just as URL params, not as a SOAP object. I'm also specifying that I'm interested in a json response with the .json at the end of the URL.

Sign up to request clarification or add additional context in comments.

5 Comments

haw i can ask my server to not wrap response in the XML and use just return the JSON
haw i can get my Id_USR=1 in my function getCOMPAINs?
getCOMPAINs is my function in sever and what u mean getCOMPAINs.json?
I'ld stick to option 1. You're already almost there, you just need to strip the additional XML from your input and then decode the html-entities from your JSON.
@Tim van Elsloo - I'm for option (1) as well - parsing that XML should be pretty simple; it would have taken less time just to parse it than it would to have all the arguments he's had on this question ;)
1

See this question: Native JSON support in iOS? , which refers to this link: http://code.google.com/p/json-framework/

8 Comments

the problem that i recive json in xml string
i not use json file but i recive string from my server
can i change sendRequestCompains to use json?
The response looks like SOAP to me. sudzc.com does a great job in generating an iPhone project, that parses SOAP responses. From there on, you can use a JSON parser. Or take a look here: stackoverflow.com/questions/5966401/…
if i not want use soap her and just use json can i do this?
|
0

Before this line:

NSArray *statuses = [parser objectWithString:json_string error:nil];

Add these lines:

json_string = [json_string stringByReplacingOccurancesOfString:@"<?xml version="1.0" encoding="ISO-8859-1"?><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/"><SOAP-ENV:Body><ns1:getCOMPAINsResponse xmlns:ns1="http://tempuri.org/PlatformService/method"><COMPAIN xsi:type="xsd:string">" withString:@""];
json_string = [json_string stringByReplacingOccurancesOfString:@"</COMPAIN></ns1:getCOMPAINsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>" withString:@""];

This is hardcoded, so you need to be sure this won't change.

What's left is the json_string, but encoded with html-entities. To decode it, add this class to your project:
https://discussions.apple.com/message/8064367#8064367

And add another line before NSArray *statuses ....

json_string = [[[MREntitiesConverter alloc] init] convertEntiesInString:json_string];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.