I am trying to get all Employee Name from my JSON Data.
This is my JSON Data
[ { "0" : "65" , "id" : "65" , "1" : "etertet" , "employee_name" : "etertet" , "2" : "1" , "employee_salary": "1" , "3" : "2" , "employee_age" : "2" } ]
This is my function of my 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); }
This is my code to get JSON Data
<?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; ?>
But I am getting the error below:
Notice: Trying to get property of non-object in C:xampphtdocscloudv1get.php on line 14**
Thank you
Advertisement
Answer
You can access as below
$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