Error while reading a Json string

0

I'm trying to create a table for a football league 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 error:

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)
    
asked by anonymous 14.10.2018 / 21:24

2 answers

0

Your json is not in the right pattern, the right thing would be to do something like this:

{"Times":[{
   "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"
                                }
                            ]
                        }
                    ]
        }
        ]
        }

Then to iterate through them just do the following;

obj.Times.forEach(function(o,index){ console.log(o.id); })
    
14.10.2018 / 22:15
0

Your JSON is missing the array definition for the players and the comma to separate one player from the other, try using the file below:

    [{
       "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'
                }]
            }]
    }]
    
15.10.2018 / 14:41