travel json file without tags

1

Hi, I have a serious problem, I have a json file with the following format:

{
    "alberto": {
        "agis": "admin",
        "lima": "admin"
    },
    "pedro": {
        "agis": "admin"
    },
    "manu": {
        "lima": "admin"
    },
    "marcos": {
        "agisweb": "office"
    }
}

and I do not know how to go through it, I'm used to the typical format:

{"poblacion":[ 
  { "id": "0", "nombre": "Alcobendas" }
  ,
  { "id": "1", "nombre": "Miraflores de la Sierra" }
  ,
  { "id": "2", "nombre": "San Fernando de Henares" }
]}

where you can go through as a population [0] .name = Alcobendas etc.

But with the first structure I am totally lost, I have managed to cross it by placing it in an object where I add tags to go through it, but I need to read it and modify it without the structure of the initial file changing and that is where I have the problem, someone has an idea How to walk it?

they had told me something like data ['alberto'] [agis] or something like that ... but nothing I have tried everything and there is no way.

work with angle 1.x in case there is any method. Thank you very much!

    
asked by Raúl Lorenzo 21.02.2017 в 02:22
source

3 answers

0

You can use a for in.

link

let list = { "alberto": { "agis": "admin", "lima": "admin" }, "pedro": { "agis": "admin" }, "manu": { "lima": "admin" }, "marcos": { "agisweb": "office" } };
    
    
    for (let key in list) {
        var element = list[key];
    
        list['alberto'].agis = 'Foo';
        
        console.log(key);
        console.log(element);   
    }
    
answered by 21.02.2017 / 02:44
source
2

You can iterate over a JS object by doing:

for (etiqueta in objeto) {
  console.log(etiqueta, objeto[etiqueta]);
}

And if you prefer to know in advance the labels of the object:

var etiquetas = Object.keys(objeto);
console.log(etiquetas);

So you can iterate over the array etiquetas the way you're familiar:

for(i=0; i<etiquetas.length; i++) {
  console.log(etiquetas[i], objeto[etiquetas[id]]);
}
    
answered by 21.02.2017 в 02:33
1

To travel a json object you can do it with Object.keys . You can get the key and its respective value.

var datos={ "alberto": { "agis": "admin", "lima": "admin" }, "pedro": { "agis": "admin" }, "manu": { "lima": "admin" }, "marcos": { "agisweb": "office" } };

Object.keys(datos).forEach(key => {
  let value = datos[key]
  console.log(key,value);
})
    
answered by 21.02.2017 в 02:38