json解析字符串 [英] json parse string

查看:116
本文介绍了json解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了php服务器,并在目标c中调用了此函数

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



  }

如果我尝试使用php调用此函数,我可以显示此响应

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"
  }
}

我如何使用Json解析此响应数据?我尝试执行此解决方案,但是将其与xml一起使用

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

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 *响应= [NSURLConnection sendSynchronousRequest:requestingResponse:nil错误:nil];

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

encoding:NSUTF8StringEncoding];

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

error:nil];

error:nil];

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

NSDictionary的状态要求 //关于日志实用程序和日志 NSLog(@%@",[状态objectForKey:@"COMPAIN_ID"]); }

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

推荐答案

您想使用JSON解析响应,但响应包装在某些XML中.

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

您有两种选择:

1)解析XML以获取JSON,然后将其解析为JSON

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

2)要求服务器不要将您的响应包装在XML中,只需返回JSON.

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

您似乎正在使用SOAP(来自sendRequestCompains)与服务器通信-如果您发出SOAP请求,它将返回JSON对象的可能性很小.我认为您最好的选择是选择(1).

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

但是,如果您可以控制服务器,那么我将完全停止使用SOAP并使其响应

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

我将参数传递为URL参数,而不是作为SOAP对象传递.我还指定对URL末尾带有 .json 的json响应感兴趣.

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.

这篇关于json解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆