我想从 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 中收到的数据。