【发布时间】:2011-07-27 10:28:54
【问题描述】:
我创建 php 服务器并在我的目标 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);
}
如果我尝试用 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">[{"COMPAIN_ID":"44"},{"COMPAIN_ID":"46"}]</COMPAIN></ns1:getCOMPAINsResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
我尝试了这个解决方案,但不起作用
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURLURLWithString:@"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 objetNSData NSData *响应 = [NSURLConnection sendSynchronousRequest:request returnedResponse:nil error:nil];
// On récupère le JSON en NSString depuis la réponse NSString *json_string = [[NSString alloc] initWithData:response编码:NSUTF8StringEncoding];
// on parse la reponse JSON NSArray *statuses = [parser objectWithString:json_string错误:无];
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"]); }
【问题讨论】:
-
请将来自您服务器的响应(JSON,而不是 PHP pritn_r 输出!)添加到问题中!下面的一个 cmets 说“在 xml 字符串中接收 json”,这根本没有意义 - 我们需要更多信息。
-
这个 json 包含在响应中 [{"COMPAIN_ID":"44"},{"COMPAIN_ID":"46"}]
-
那里根本没有 XML,你为什么说 'xml string'?
-
因为收到的响应以
开头 -
所以您在之前的评论中发布的代码不是完整的回复!为什么不能解析 XML 得到 JSON,然后只解析 JSON?
标签: objective-c json