0

我想从 PHP 网络服务发送的 iOS 中的数组中检索数据。以下是数组的结构:

[
    {
        "fullname": "Kate Bell",
        "email": "kate-bell@mac.com"
    },
    {
        "fullname": "Kate Bell",
        "email": "www.creative-consulting-inc.com"
    },
    {
        "fullname": "Daniel Higgins",
        "email": "d-higgins@mac.com"
    },
    {
        "fullname": "John Appleseed",
        "email": "John-Appleseed@mac.com"
    }
]

注意:数组可以是任意长度。

PHP代码:

mysql_select_db("mydb");

    $ReturningArray = array();

    foreach($Contacts_Array as $arr)
    {
        foreach($arr['emails'] as $email_address)
        {
            $query = "select email from table where email='".$email_address."'";
            $result = mysql_query($query);

            $row = mysql_fetch_assoc($result);

            if(empty($row))
            {
                $Contact = array(
                "fullname" => $arr['fullname'],
                 "email"   => $email_address
                 );

                $ReturningArray[] =  $Contact;
            }
        }

    }

    echo json_encode($ReturningArray);

更新:

我已经尝试过这段代码,但它不起作用

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        // NSString *Result = [[NSString alloc] initWithData:data      encoding:NSUTF8StringEncoding];
   // NSLog(@"Result in NonTroopeUsers : %@",Result);

        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        NSLog(@"COUNT :%d",[array count]);

        for (NSDictionary* item in array)
        {
            NSString *fullname = [item objectForKey:@"fullname"];
            NSString *email = [item objectForKey:@"email"];

            NSLog(@"Full name: %@",fullname);

            NSLog(@"email: %@",email);
        }

    }

数组计数为零,但注释代码显示在 nslog 中收到的数据。

4

3 回答 3

0

出色地

id responseArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(!error){
  if([responseArray isKindOfClass:[NSArray class]])
     for(id entry in responseArray){
        if([entry isKindOfClass:[NSDictionary class]]){
         // do what you want with the dictionary
       }
     }
   }
 }
于 2013-06-12T07:39:10.257 回答
0

不要在connection: didReceiveData:方法中执行转换。我们可能只有部分数据。这可能会被多次调用以提供大量数据。您必须添加这些部分数据块才能获得完整的数据。

您可以使用 NSJSONSerialization 来解析 JSON。

NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

还,

创建成员变量

 NSMutableData *netData;

实现以下方法

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
     if(netData == nil)
          netData = [[NSMutableData alloc]init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [netData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
      //// Here convert the Full Data to JSON, 
     NSArray *array = [NSJSONSerialization JSONObjectWithData:netData options:kNilOptions error:nil];
     NSLog(@"%@",array);


}
于 2013-06-12T07:20:55.587 回答
0

我已经解决了这个问题。我在 php 文件的开头使用了 var_dump($SomeVariable) 。当我删除它时,我能够检索数据。

于 2013-06-13T04:28:37.590 回答