Probably the data passed on json_decode
is not a valid JSON format string, so there must have been some error while decoding and it won’t work.
If the variable $obj
is a string, it should contain a format like:
$obj = '{...}';
If it came from a file probably the read file is invalid or this at the wrong address, if $obj
came from extract($_POST);
this may be a flaw in your HTML form, anyway the problem is the format of JSON.
And another detail, echo
does not display the object, only displays number and strings, or __toString
, recommend using var_dump
Example test that works
I suppose the form should look like this:
<form action="listaluno.php" method="POST">
<textarea name="obj">
[
{
"StrNome": "Nome",
"NumMatricula": 10000,
"DtNascimento": "10/10/2001",
"NumIdUop": 1
},
{
"StrNome": "Foo Bar",
"NumMatricula": 3000,
"DtNascimento": "10/10/2002",
"NumIdUop": 2
}
]
</textarea>
<button>Enviar</button>
</form>
And then try to click Send, should work, the test resulted in:

Errors in the JSON
Note that there may have been some failure in the format of your JSON, such as an extra comma:
[
{
"StrNome": "Nome",
"NumMatricula": 10000,
"DtNascimento": "10/10/2001",
"NumIdUop": 1
},
{
"StrNome": "Foo Bar",
"NumMatricula": 3000,
"DtNascimento": "10/10/2002",
"NumIdUop": 2
}, <-------------------- Virgula extra
]
If there is any fault and do this:
<?php
$foo = '[
{
"StrNome": "Nome",
"NumMatricula": 10000,
"DtNascimento": "10/10/2001",
"NumIdUop": 1
},
{
"StrNome": "Foo Bar",
"NumMatricula": 3000,
"DtNascimento": "10/10/2002",
"NumIdUop": 2
},
]';
var_dump(json_decode($foo));
Will return this:
NULL
Like debug JSON
In case you are completely sure that your json was in the correct format, then redo your script like this:
<?php
extract($_POST);
$genList = json_decode($obj);
if (!$genList) {
switch (json_last_error()) {
case JSON_ERROR_DEPTH:
echo 'A profundidade máxima da pilha foi excedida';
break;
case JSON_ERROR_STATE_MISMATCH:
echo 'JSON malformado ou inválido';
break;
case JSON_ERROR_CTRL_CHAR:
echo 'Erro de caractere de controle, possivelmente codificado incorretamente';
break;
case JSON_ERROR_SYNTAX:
echo 'Erro de sintaxe';
break;
case JSON_ERROR_UTF8:
echo 'Caractere UTF-8 malformado, codificação possivelmente incorreta';
break;
default:
echo 'Erro desconhecido';
break;
}
exit;
}
var_dump($genList);
?>
<table class= "table table-striped">
<tbody>
<thead>
</thead>
<?php foreach($genList as $aluno){ ?>
<?php
$scampos = $aluno->StrNome. "|".$aluno->NumMatricula. "|" .$aluno->DtNascimento. "|" .$aluno->NumIdUop;
?>
<tr campos="<?php echo $scampos; ?>">
<td><?php echo $aluno->StrNome; ?></td>
<td><?php echo $aluno->NumMatricula; ?></td>
<td><?php echo $aluno->DtNascimento; ?></td>
<td><?php echo $aluno->NumIdUop; ?></td>
<td><a href="#" class="editar_aluno"><span class="glyphicon glyphicon-pencil" title="Editar Aluno"></span></a></td>
<td><a href="#" class="excluir_aluno"><span class="btTabela glyphicon glyphicon-trash" title="Excluir Aluno"></span></a></td>
</tr>
<?php } ?>
</tbody>
</table>
Note: I listed only a few errors, however in PHP7 there are more errors http://php.net/manual/en/function.json-last-error.php
I suggest you remove your email from the question, this kind of thing attracts all kinds of spam! As to your question, the error refers to a loop
foreach
malformed, but in the code that posted there is noforeach
. That’s all your codelistaluno.php
?– nunks
Put the full foreach in the question as it stands it is not clear the mistake
– WMomesso
The code that was missing was due to formatting the publication... Welcome Aline, the problem is not in the
foreach
yes at the time you are sending the form, edit the question and put the form code.– NoobSaibot