Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  72] [ 3]  / answers: 1 / hits: 16550  / 13 Years ago, fri, august 26, 2011, 12:00:00

I am unable to access a json value



{phone: [
{
@attributes: {
type: cell,
ext:
}
}, (123) 456 7890, {
@attributes: {
type: work,
ext:
}
}
]}


using the following JavaScript: psudo



for each phone line ...

console.log([@attributes].type);
console.log(this);
console.log([@attributes].ext);

... end for


I expected the following output:



cell
work (123) 456 7890

More From » json

 Answers
22

actually your json structure is not perfect, so here is the solution for your desired output



var json = {phone: [
{
@attributes: {
type: cell,
ext:
}
}, (123) 456 7890, {
@attributes: {
type: work,
ext:
}
}
]};
console.log(json['phone'][0]['@attributes'].type);
console.log('<br/>'+json['phone'][1]);
console.log('<br/>'+json['phone'][2]['@attributes'].type);


DEMO


[#90404] Wednesday, August 24, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austynp

Total Points: 505
Total Questions: 118
Total Answers: 106

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
austynp questions
;