0

I try to call a JSON link and display to PHP but failed. The error state that:

"Uncaught Error: Cannot use object of type stdClass as array"

Below is the result of JSON (http://172.20.0.45/TGWebService/TGWebService.asmx/factoryList)

enter image description here

Below is my current PHP code

<!DOCTYPE html>
<html lang="en">

<?php

$url = 'http://172.20.0.45/TGWebService/TGWebService.asmx/factoryList'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed

echo $characters[0]->facID;

foreach ($characters as $character) {
    echo $character->facID . '<br>';
}

?>

<table>
    <tbody>
        <tr>
            <th>Fac ID</th>
            <th>Fac Name</th>
        </tr>
        <?php foreach ($characters as $character) : ?>
        <tr>
            <td> <?php echo $character->facID; ?> </td>
            <td> <?php echo $character->facName; ?> </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

</html>

Let say if I want to display, all facID and facName, how to do that?

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56

0 Answers0