Show contents of a JSON using PHP and separate the fields

1

I have the URL of an API that generates a JSON. I have this code below, however I need the separate fields so I can create while of the items and separate type echo $row['nome_marca'] and so on. Any light?

<?php
header("Content-Type: application/json");
$jsonData = file_get_contents("http://www.folhacar.com.br/frontendnovo.php/api/listMarcas");
echo $jsonData;
?>
    
asked by anonymous 10.07.2015 / 21:55

2 answers

4

To read data from a JSON you can use the json_decode function. See:

<?php
    $json = json_decode(file_get_contents("http://www.folhacar.com.br/frontendnovo.php/api/listMarcas"));
    for($i = 0; $i < count($json); $i++) {
        echo "<div>ID: " . $json[$i]->{'marca_id'} . "</div>";
        echo "<div>Marca: " . $json[$i]->{'nome_marca'} . "</div>";
        echo "<br />";
    }
?>

The json_decode function returns an array. Then just use for () to traverse it and go through the data.

    
10.07.2015 / 22:27
0

Use the json_decode PHP function to turn your json string into something more readable in PHP.

In this your output (json) would be an array

link

<?php
header("Content-Type: application/json");
$jsonData = file_get_contents("http://www.folhacar.com.br/frontendnovo.php/api/listMarcas");
$arrData = json_decode($jsonData); // Transforma o seu JSON
// print_r($arrData);

foreach ($arrData as $indiceDoArray => $valorDoArray) {
    echo "Elemento na posição {$indiceDoArray} tem valor {$valorDoArray}<br>";
}
?>
    
10.07.2015 / 22:19