Having an issue here. I need to loop through the attributes of a JSON structure, like the one shown below:
var txt = '{"employees":{' +
'"p1":{'+
'"info":{'+
'"firstName":"John","lastName":"Doe" }},' +
'"p2":{'+
'"info":{'+
'"firstName":"Anna","lastName":"Smith" }},' +
'"p3":{'+
'"info":{'+
'"firstName":"Peter","lastName":"Jones" }}'+
'}}';
I'm looking for a way to achieve this without using specific p values like p1, p2, p3 in my code.
json.employees.p1.info.firstName;
This is not the approach I want to take. Instead, I'm aiming for something similar to this:
for(var i = 0; i<3;++i){
console.log(json.employees.p[i].info.firstName);
}
Any ideas on how to accomplish this?
The reason behind this requirement is that the attribute p can be any number (N), so hardcoding p1, p2, p3, etc., isn't feasible.