2011-07-27 202 views
1

我创建PHP的服务器,我调用这个函数在我的目标CJSON解析字符串

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); 



    } 

,如果我尝试调用与PHP的这个功能,我可以表明这种反应

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

如何可以解析使用JSON这个响应数据?我试图做到这一点的解决方案,但这种使用带有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; 
    } 

我尝试这个解决方案,但不是活像ķ

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> 

我尝试这个解决方案,但没有工作

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"]); }

+0

请将您的服务器(JSON,而不是PHP pritn_r输出!)的响应添加到问题中!其中一条评论如下所示:“recive json in xml string”并且毫无意义 - 我们需要更多信息。此 – deanWombourne

+0

的JSON includ响应 [{" COMPAIN_ID ":},{" COMPAIN_ID ":}] –

+0

没有我的帮助? –

回答

3

您想使用JSON解析响应,但响应被封装在一些XML中。

你有两个选择:

1)解析XML得到JSON,然后解析为JSON

2)要求服务器不换行的XML您的回复,只返回JSON 。

您似乎正在用SOAP(来自sendRequestCompains)与服务器交谈 - 如果您发出SOAP请求,它将返回一个JSON对象的机会并不多!我认为你最好的选择是选项(1)。

但是,如果你有在你的服务器控件我会停止使用SOAP不惜一切,并得到它的回应像

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

我传递的paramters一样网址参数,而不是作为一个SOAP目的。我还指定我对URL的末尾有10 .json的json响应感兴趣。

+0

thx bro我想尝试选项2,实际上我不想使用XML –

+0

haw我可以让我的服务器不要在XML中包装响应,并使用刚才返回的JSON –

+0

haw我可以让我的函数getCOMPAINs中的Id_USR = 1? –

0

这一行之前:

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

添加这些行:

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:@""]; 

这是硬编码的,所以你必须确保这不会改变。

剩下的是什么是json_string,但与HTML实体编码。对其进行解码,将该类添加到您的项目:
https://discussions.apple.com/message/8064367#8064367

而且NSArray *statuses ...前添加另一条线。

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