gpt4 book ai didi

javascript - PHP: Json 获取个人数据

转载 作者:行者123 更新时间:2023-12-04 10:55:04 25 4
gpt4 key购买 nike

我正在尝试从我的 JSON 数据中获取所有员工姓名。

这是我的 JSON 数据

[ { "0"              : "65"
, "id" : "65"
, "1" : "etertet"
, "employee_name" : "etertet"
, "2" : "1"
, "employee_salary": "1"
, "3" : "2"
, "employee_age" : "2"
}
]

这是我的 JSON 函数:
function get_employees($id=0)
{
global $connection;
$query="SELECT * FROM employee";
if($id != 0)
{
$query.=" WHERE id=".$id." LIMIT 1";
}
$response=array();
$result=mysqli_query($connection, $query);
while($row=mysqli_fetch_array($result))
{
$response[]=$row;
}
header('Content-Type: application/json');
echo json_encode($response);
}

这是我获取 JSON 数据的代码
<?php
# An HTTP GET request example

$url = 'http://localhost/cloud/v1/employees';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
//echo $data;
$obj = json_decode($data);
echo $obj->employee_name;
?>

但我收到以下错误:

Notice: Trying to get property of non-object in C:\xampp\htdocs\cloud\v1\get.php on line 14**



谢谢

最佳答案

您可以通过以下方式访问

$ar = '[{"0":"65","id":"65","1":"etertet","employee_name":"etertet","2":"1","employee_salary":"1","3":"2","employee_age":"2"}]
';



$data = json_decode($ar);

echo $data[0]->employee_name

关于javascript - PHP: Json 获取个人数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com