I'm having a problem when traversing a JSON.
I show you the code.
client.search(buscar).then(images => {
let imgjson = JSON.stringify(images, ["url"], 4);
METODO.PARA.MOSTRAR(imgjson);
});
Well, I explain.
what I do is use an api that returns a json, I pass search , and I receive images .
When I show images that's what I get [obj] [obj], so I put imgjson in JSON.stringify with the data interesting ("url"). Now when I want to show the result of imgjson I receive the following
[
{
"url": "http://www.vuescript.com/wp-content/uploads/2017/02/Vue-JSON-Tree-View.png"
},
{
"url": "https://www.w3resource.com/w3r_images/json-introduction.png"
}
]
(shortened) What I get is exactly what I searched for, I searched for the word "json" in google and I received several images. So far all great.
We can take for granted that ("" imaginary "")
let imgjson = [
{
"url": "http://www.vuescript.com/wp-content/uploads/2017/02/Vue-JSON-Tree-View.png"
},
{
"url": "https://www.w3resource.com/w3r_images/json-introduction.png"
}
];
right? because the JSON.stringify is the same as that.
Now, when I want to go through imgjson I should do
imgjson[0].url
and should receive the first link of the object.
The problem is that I do not receive the first link, only I get undefined.
This is how it works:
let imgjson = [
{
"url": "http://www.vuescript.com/wp-content/uploads/2017/02/Vue-JSON-Tree-View.png"
},
{
"url": "https://www.w3resource.com/w3r_images/json-introduction.png"
}
]
//METODO PARA MOSTRAR
document.write(imgjson[0].url)
But not like that:
//hagamos de cuenta que recibo el JSON images
let imgjson = JSON.stringify(images, ["url"], 4);
//metodo para mostrar
document.write(imgjson[0].url);
This last shows "undefined", but imgjson shows the json above.
Sorry if it was a bit chaotic, but I want to make sure it is understood. Does anyone know why the latter does not work if in theory the let imgjson is equivalent to the JSON?
Thank you very much for your time.