Difficulty accessing json

0

I have the following json:

            {
  "destination_addresses": [
    "Rua B, 1 - Coqueiro,Belém - PA, 66670-350, Belém - PA, 66670-350, Brasil"
  ],
  "origin_addresses": [
    "Pref. José Walter, Fortaleza - CE, 60810-670, Brasil"
  ],
  "rows": [
    {
      "elements": [
        {
          "distance": {
            "text": "1.495 km",
            "value": 1495361
          },
          "duration": {
            "text": "20 horas 33 minutos",
            "value": 74009
          },
          "status": "OK"
        }
      ]
    }
  ],
  "status": "OK"
}

My difficulty is basic I know, but I can not access the json nodes when I try

data.destination_addresses returns me exactly

["Rua B, 1 - Coqueiro,Belém - PA, 66670-350, Belém - PA, 66670-350, Brasil"]

and not just the value .. Rua B, 1 - Coqueiro,Belém - PA, 66670-350, Belém - PA, 66670-350, Brasil

I also can not access for example the distance or duration already tried data.rows.elements.distance and data.rows[0].elements.distance but also can not .. what is missing so that I can access the nodes?

obs. I use vues.

    
asked by anonymous 14.06.2017 / 14:58

2 answers

2
  

Edited: Complement:

  Note that Elements is also a rray of objects ("distance" and "duration") and that this array ( Elements ) is inside another array that is rows , so in order to access an object of Elements will have to do an iteration in row and in Elements , something like this (python as inspiration)
for row in data.rows:
    for element in row.elements:
       print (element.distance)
       #...

When you access data.destination_addresses return is exactly what the json author wanted to express, an array of strings, probably some addresses should have more than one string (more than one address). See data.rows is also an array, so you would have to access it like this:

drows = data.rows[0]

And then 'scan' drows.

    
14.06.2017 / 15:13
-1

Correcting the comment according to the observation below, there is no error in the array, the form presented is correct, I was able to access the data of the array elements without major problems, but it is necessary to indicate the index of the objects inside the array console.log(json.rows[0].elements[0].duration.value);

var json = {
  "destination_addresses":
    ["Rua B, 1 - Coqueiro,Belém - PA, 66670-350, Belém - PA, 66670-350, Brasil"],
  "origin_addresses":
    ["Pref. José Walter, Fortaleza - CE, 60810-670, Brasil"],
  "rows":[
    {"elements":
        [{
          "distance": {"text": "1.495 km", "value": 1495361},
          "duration": {"text": "20 horas 33 minutos", "value": 74009 },
          "status": "OK"
        }]
    }
  ],
  "status": "OK"
}
console.log(json.rows[0]);
console.log(json.rows[0].elements[0]);
console.log(json.rows[0].elements[0].duration);
console.log(json.rows[0].elements[0].duration.value);
    
14.06.2017 / 16:14