使用AFNetworking将NSDictionary与NSDrray NSDurary一起发布

时间:2014-02-19 21:08:56

标签: php ios json afnetworking

我使用NSDictionary框架通过帖子NSArray发送NSDictionaries AFNetworking NSArray。我想知道,如何在PHP中创建一个服务来从{ people = ( { address = "N/A"; email = "N/A"; lastname = Borgen; name = Dan; phone = "(555)\U00a0555-555"; zip = "N/A"; }, { address = "N/A"; email = "N/A"; lastname = Moose; name = Sara; phone = "(444)\U00a0444-444"; zip = "N/A"; }, { address = "N/A"; email = "N/A"; lastname = Roth; name = Jordan; phone = "(333)\U00a0333-333"; zip = "N/A"; }, { address = "N/A"; email = "N/A"; lastname = Wise; name = Gilbert; phone = "(444)\U00a0444-444"; zip = "N/A"; }, { address = "N/A"; email = "N/A"; lastname = Tendy; name = Sheila; phone = "(222)\U00a0222-222"; zip = "N/A"; } ); 调用人员?获取数据,接下来NSDictionary发布了:

if ([NSJSONSerialization isValidJSONObject:array]) {


    NSDictionary *dict=@{@"people":array};




    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://www.myservice.com"]];


    [httpClient setDefaultHeader:@"Content-Type" value:@"application/json"];


    [httpClient setParameterEncoding:AFFormURLParameterEncoding];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
                                                            path:@"/mobile/myservice.php"
                                                      parameters:dict
                                    ];

           AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {


        if (JSON !=nil) {...}

然后是iOS中使用的代码:

      $json = $_POST['people'];
      $obj = json_decode($json, true);  
      foreach( $obj as $person ): 
      $first = $person['name'];
  $last = $person['lastname'];
  $zip = $person['zip'];
  $address = $person['address'];
  $phone = str_replace("]","",str_replace("[","",str_replace(".","",str_replace("-    ","",$person['phone']))));
  $phone = str_replace(" ","",$phone);
  $phone = str_replace("+","",$phone);
  $email = $person['email'];

     /*php code to insert registers in database */

     endforeach; 

为后端编写的代码是:

{{1}}

0 个答案:

没有答案