Error while reading a Json string

Asked

Viewed 60 times

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?

  • I copied wrong, already corrected.

  • 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); });

  • I added one more Team to Json, there may be several teams.

2 answers

0

Your JSON is missing the array setting for 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'
                }]
            }]
    }]

0

Your json is not in a correct pattern, the right thing would be to do something more or less 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 for them just do the following;

obj.Times.forEach(function(o,index){ console.log(o.id); })

Browser other questions tagged

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