How to read the {1x2} JSON Jquery key

Asked

Viewed 52 times

-1

Gentlemen, excuse my ignorance but how can I read a JSON key that starts in number, example:

"1x2": {
     "outright": false,
     "interwetten": {
          "odds": {
                "1": 6.4,
                "2": 1.55,
                "X": 3.95
           },
           "bookmaker": true,
           "exchange": false,
           "name": "Interwetten"
      },
      "bwin": {
          "odds": {
                "1": 6.5,
                "2": 1.5,
                "X": 3.8
          },
           "bookmaker": true,
           "exchange": false,
           "name": "bwin"
},
.....

I’m doing the reading as follows:

$.getJSON("MEUJSON", function(retorno){ 
.each(retorno, function (i) {
     // console.log(retorno);
     var data = retorno[i].event.start_time;
     $('.tabela').append(retorno[i].event.away);
     $.each(retorno[i].sites, function(a) {
         //AQUI SERIA A LEITURA DA CHAVE
         console.log(retorno[i].sites[a].\`1x2`\);
     })
})
})

Thanks in advance!

2 answers

0

You can access this way

var meuObj = {"1x2": {
     "outright": false,
     "interwetten": {
          "odds": {
                "1": 6.4,
                "2": 1.55,
                "X": 3.95
           },
           "bookmaker": true,
           "exchange": false,
           "name": "Interwetten"
      },
      "bwin": {
          "odds": {
                "1": 6.5,
                "2": 1.5,
                "X": 3.8
          },
           "bookmaker": true,
           "exchange": false,
           "name": "bwin"
    }
}

var valor1x2 = meuObj["1x2"];

If you are an array of objects, you must access the object first in order to access its values.

Just one observation in the reading

The .each jquery, besides delivering the indices, tmb gives you the iterated element in the loop memento.

$.getJSON("MEUJSON", function(retorno){ 
.each(retorno, function (i, ele) {
     // console.log(retorno);
     var data = ele.event.start_time;
     $('.tabela').append(ele.event.away);
     $.each(ele.sites, function(a, site) {
         //AQUI SERIA A LEITURA DA CHAVE
         console.log(site['1x2']);
     })
   })
})

With this it is dispensable to call every time the root object and go decenting the levels according to the hierarchy, as in the case retorno[i].sites[a]['1x2'] was left only site['1x2'], because the website array is already being iterated

  • Got it, in case my JSON it has other keys, and it’s inside a $.each, I’m doing like this: $. getJSON("MEUJSON", Function(return){ $.each(return, Function (i) { // console.log(return); var data = return[i].event.start_time; $('. table'). append(return[i].event.away); $.each(return[i]. sites, Function(a) { //HERE WOULD BE THE READING OF THE KEY console.log(return[i]. sites[a]. `1x2`); }) }) })

  • Try it this way console.log(retorno[i].sites[a]['1x2'] replacing that part retorno[i].sites[a].`1x2 The difference is that you do not access with the . and yes with the clasps []

  • Good morning, I tried and did not roll, it returns me -> Undefined.

  • Good morning, so it’s because inside the object retorno[i].sites[a] there is no direct key called '1x2', if you can send the full structure of json, at least this part of the access to site, because I think this object is not directly on site. If you can send the complete structure of site, it will be easier to help.

0

This key is a String, so you can use as a basis one of the examples below:

//Usando jquery json parser
var obj = $.parseJSON('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');
alert(obj['jobtitel']);

//Usando javasript json parser
var t = JSON.parse('{"name": "", "skills": "", "jobtitel": "Entwickler", "res_linkedin": "GwebSearch"}');
alert(t['jobtitel'])

Browser other questions tagged

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