Reputation: 35
I am trying to parse a json file and show its content in table using php, but for some reason I am getting (Notice: Undefined property: stdClass::$managed_status in z.json)
so here is my a full example of what I have in my json file (z.json):
{ "metadata": { "total_record_count": "1" }, "devices": [ { "managed_status": "2", "device_id": "63225421111", "is_supervised": true, "os_version": "10", "is_lost_mode_enabled": false, "serial_number": "77898789", "device_type": "1", "owned_by": "1", "is_removed": "false", "product_name": "TC26", "device_name": "[email protected]", "platform_type": "android", "located_time": "1623936014747", "imei": [ "352714110033545" ], "model": "TC26", "customer_name": "Customer1", "customer_id": "10489000000041017", "udid": "95bd2e6ca8654910", "last_contact_time": "1624254859593", "platform_type_id": "2", "user": { "user_email": "[email protected]", "user_id": "556654548", "user_name": "[email protected]" }, "device_capacity": "17.34" } ], "delta-token": "aHR0cHM6Ly9tZG0ubWFuYWdlZW5naW5lLmV1L2FwaS92MS9kZXZpY2VzOjoxNjI0MjU1NzA2NDI4" }
and here is my php script:
<?php
$json=file_get_contents('z.json');
$data=json_decode($json);
?>
<table>
<tr>
<td>managed_status</td>
<td>device_id</td>
<td>is_supervised</td>
<td>os_version</td>
</tr>
<?php foreach($data as $key=>$item): ?>
<tr>
<td><?PHP echo $item->managed_status ; ?></td>
<td><?PHP echo $item->device_id; ?></td>
<td><?PHP echo $item->is_supervised; ?></td>
<td><?PHP echo $item->os_version; ?></td>
</tr>
<?php endforeach; ?>
</table>
Upvotes: 2
Views: 505
Reputation: 169
In the foreach condition you are trying to iterate over the date, you need to replace that with $data->devices.
<?php
$json = file_get_contents('z.json');
$data = json_decode($json);
?>
<tr>
<td>managed_status</td>
<td>device_id</td>
<td>is_supervised</td>
<td>os_version</td>
</tr>
<?php foreach($data->devices as $key => $item): ?>
<tr>
<td><?php echo $item->managed_status ; ?></td>
<td><?php echo $item->device_id; ?></td>
<td><?php echo $item->is_supervised; ?></td>
<td><?php echo $item->os_version; ?></td>
</tr>
<?php endforeach; ?>
Upvotes: 3