我正在尝试使用PHP对我的SQL SELECT语句的结果进行JSON编码,并且我不确定在编码之前是否正确格式化了我的数组。
我的PHP代码是:
$stmt = $this->db->prepare('SELECT CLINIC.clinic_name AS "clinicname" FROM CLINIC ORDER BY CLINIC.clinic_name ASC');
$stmt->execute();
$stmt->bind_result($clinicname);
$test = array();
while($stmt->fetch()){
$tempArray = array('clinicname' => $clinicname);
array_push($test, $tempArray);
}
$stmt->close();
// Return clinics, encoded with JSON
header('Content-type: application/json');
$json = json_encode($test);
echo $json;
此阵列创建和编码的结果是:
[{"clinicname":"Bangor"},{"clinicname":"Belfast"},{"clinicname":"Crumlin"},{"clinicname":"Londonderry"}]
所以我有一个数组数组。
这可以作为JSON发送吗?我看到的所有示例似乎都是单个数组。到目前为止我是否正确?
然后,关于iOS,收到的对象是NSDictionary还是NSArray?
任何有关上述内容的帮助或反馈都将受到极大的欢迎。
答案 0 :(得分:0)
答案 1 :(得分:0)
在iOS中,如果这是来自http请求的结果,它将是NSString类型,然后需要通过以下方式解析:
然后将其转换为NSArray,NSDictionary NSString等
答案 2 :(得分:0)
是的,您可以发送一组数组,您可以按照以下方式访问:
$response = json_decode($response,true);
foreach ($response as $resp)
{
echo "<br>";
print_r($resp);
}
json示例:
[{"id":"38","first_name":"prueba","second_name":"example","first_surname":"prueba","second_surname":"GONZALEZ","email":"x@x.net","network_login":"x.x","pos_name":"x x","position":"1","active":"1"},{"vhur":"292","first_name":"c","second_name":"example","first_surname":"example","second_surname":null,"email":"example@example.com","network_login":"example.example","pos_name":"example example","position":"2","active":"1"}]