SOLVED - How to read this JSON in PHP

Asked

Viewed 37 times

-4

Gentlemen, I am receiving the JSON via URL and I try to recover the values via PHP, but always this returning me the error:

Warning: foreach() argument must be of type array|Object, string Given in C: xampp htdocs exams Adm tests.php on line 25

PHP

$jsonTeste = '{"exames": [
{
            "nomeExame": "",
            "descricaoExame": "",
            "valorExame": 0.00,},{
            "nomeExame": "MAPA - 24 HORAS",
            "descricaoExame": "",
            "valorExame": 80.00,},{
            "nomeExame": "HOLTER - 24 HORAS",
            "descricaoExame": "",
            "valorExame": 80.00,}],
    "sucesso": "true",
    "mensagemErro": "Requisição efetuada com sucesso"
  }';

  $json = json_decode($jsonTeste, TRUE);
   foreach($json as $row ){
    echo $row->nomeExame;
   }

   

RESOLVED I was receiving the JSON with errors, with the preg_replace('/[:cntrl:]]/', ', $conteudo); I managed to solve it, staying like this:

$conteudo = file_get_contents('minha_url');
$result = json_decode($conteudo);
$json = preg_replace('/[[:cntrl:]]/', '', $conteudo);
$json = json_decode($json, true);
  echo "<table>";
  echo "<tbody>"; 
foreach($json["exames"] as $exame){
      echo "<tr>";
      echo "<td>" . $exame["nomeExame"] . "</td>";
      echo "<td><small>" . $exame["descricaoExame"] . '</small></td>';
      echo "</tr>";
  }
echo "</tbody>";
echo "<table>";
  • The conversation went on and on moved to the chat - Anyone who wants to follow or add something can use the link provided.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.