Difficulty accessing json

Asked

Viewed 206 times

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’m not getting access to 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

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

Obs. utilizo vuejs.

  • data.destination_addresses gives you an array, that’s useful if you want to divide by lines. Is that the case? And what is data.rows.elements? What HTML do you want to insert data into?

  • Hello @Sergio data.rows.elements I am trying to print this node on the page type `{{data.rows.Elements}} but it is Undefined

  • But you must have one v-for somewhere right? Make your Vue component clearer.

  • @Sergio, look what I’m trying to do here https://jsfiddle.net/26utr8wd/

  • I haven’t had time to see it yet. Tomorrow I’ll take a look if you haven’t solved the problem.

  • @Sergio would like it, yeah

Show 1 more comment

2 answers

2


Edited: Article:

Note that Elements is also a rray of objects ("Distance" and "Duration") and that this array (Elements) is within another array that is rows, then for you to access an object of Elements you’ll have to iterate on row and in Elements, something more or less like this (python as inspiration)
for row in data.rows:
    for element in row.elements:
       print (element.distance)
       #...

When you access data.destination_addresses the 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 that data.rows is also a matrix, so you would have to access so:

drows = data.rows[0]

And then 'analyze' drows.

0

Correcting the comment according to the observation below, there really is no error in the array, the form presented is correct, I was able to access the data of the array elements no major problems, but it is necessary to indicate the index of objects within 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);

  • Elements is an array of objects, with only one element (in the example), this element is an object that contains 2 'sub-objects', distance and duration. In my view there is no mistake.

  • You’re right, I interpreted it as if he wanted to put several elements inside the array. I redid my test I was able to access them.

  • But I believe that the problem he was having to access the data of the objects continues, because as the object is inside the array Elements he would need to search them indicating the index in the same way he did in rows. Instead of data.rows[0].elements.distance would be data.rows[0].elements[0].distance. Correct?

  • Without knowing the context, I still think that the ideal would be to make an iteration in elements, to travel all elelentos (that name must have been intentional).

Browser other questions tagged

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