这是我的PHP:
$query->bind_param("s", $consulta);
$result = $query->execute();
$query->bind_result($row1, $row2);
if($result)
{
$json_response = array();
while ($query->fetch())
{
$json_response[] = array("NUMERO" => $row1, "IMAGEN" => $row2);
}
}
echo json_encode($json_response);
这是我的安排:
[
{"NUMERO":99512233,”IMAGEN”:”\/imagen.jpg”},
{“NUMERO":99887766,”IMAGEN”:”\/imagen.jpg”},
{“NUMERO”:99557744,”IMAGEN”:”\/imagen.jpg”}
]
因此,我尝试阅读该安排:
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parse_json = json
{
print(parse_json[“NUMERO”][0]) //99512233
}
我该怎么读?
最佳答案
您显示的JSON:
[
{"NUMERO":99512233,”IMAGEN”:”\/imagen.jpg”},
{“NUMERO":99887766,”IMAGEN”:”\/imagen.jpg”},
{“NUMERO”:99557744,”IMAGEN”:”\/imagen.jpg”}
]
...在我看来就像一个包含多个字典的数组。
尝试将JSON输出转换为所需的类型:
let json = try JSONSerialization.jsonObject(with: data!,
options: .mutableContainers)
guard let parse_json = json as? [[String:Any]] else {
print("Unable to cast JSON to expected type")
return
}
print(parse_json[0][“NUMERO”]) //99512233