0
I’m trying to create a table for a football championship with the return of a json:
To facilitate I will put the Json below refers only to a Team:
{
"id": "1",
"nome": "Palmeiras",
"Atleta": [
{
"id": "0",
"nome": "Fernando Prass",
"idade": "1",
"FichaTecnica": [
{
"id": "1",
"posicao": "goleiro",
"camisa": "12"
},
{
"id": "2",
"posicao": "zaga",
"camisa": "11"
},
{
"id": "3",
"posicao": "meia",
"camisa": "9"
},
{
"id": "4",
"posicao": "zaga",
"camisa": "7"
},
{
"id": "5",
"posicao": "meia",
"camisa": "5"
},
{
"id": "6",
"posicao": "ataque",
"camisa": "3"
}
]
}
]
}{
'id': '2',
'nome': 'Conrinthias',
'Atleta': [
{
'id': '0',
'nome': 'Cássio',
'idade': '1',
'FichaTecnica': [
{
'id': '1',
'posicao': 'goleiro',
'camisa': '1'
},
{
'id': '2',
'posicao': 'zaga',
'camisa': '22'
},
{
'id': '3',
'posicao': 'meia',
'camisa': '5'
},
{
'id': '4',
'posicao': 'zaga',
'camisa': '7'
},
{
'id': '5',
'posicao': 'meia',
'camisa': '8'
},
{
'id': '6',
'posicao': 'ataque',
'camisa': '6'
}
]
}
]
}
The Code to read Json is as follows:
@section Scripts{
<script type="text/javascript">
$(document).ready(function () {
var valorRetornado= //É o Json exemplificado acima.
// convertendo a string em objeto
var obj = JSON.parse(valorRetornado);
obj.forEach(function (o, index) {
console.log(o.id);
});
});
</script>
}
The problem I’m facing is the following mistake:
Uncaught ReferenceError: valorRetornado is not defined
at HTMLDocument.<anonymous> (Index:61)
at fire (jquery-1.10.2.js:3062)
at Object.fireWith [as resolveWith] (jquery-1.10.2.js:3174)
at Function.ready (jquery-1.10.2.js:447)
at HTMLDocument.completed (jquery-1.10.2.js:118)
where you got the variable value?
– Lucas Miranda
I copied wrong, already corrected.
– hard123
its obj is not eternal so it does not have the foreach function, only the athletes from inside it, so: obj.Atleta.foreach(Function(o,index){ console.log(o. id); });
– Lucas Miranda
I added one more Team to Json, there may be several teams.
– hard123