0

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
1
  • @diEcho.. sorry about the {} cut! Commented Aug 26, 2011 at 10:03

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

since phone is an array, try this,

   for(var i=0;i<phone.length;i++)
     console.log(phone[i].["@attributes"].type);

Also surround your response with {, as it is currently an invalid json.

6 Comments

I'm trying to access the phone number. which is the 'value' of the 'this'.
@ is not a valid character for an JavaScript identifier. You have to use the bracket notation and a string : console.log(phone[i]["@attributes"].type)
@tnt your json is invalid. After writing a proper json, validate it from jsonlint.com
@adeel the json comes from a php json_encode the origional xml is <phone type="cell" ext=""></phone> <phone type="work" ext="">(123) 456 7890</phone>
its better if you write a custom json
|
0

I'm pretty sure you can't start any object, associative array key or well anything with a non-alphanumerical character.

Also you have 3 calls to console and only two lines of expected output? What does console.log(this) output?

4 Comments

Actually you can. Object properties (there is no such thing as a "associative array" in JavaScript) can be any string.
@dave thats what i'm trying to access it's the phone_number part of the json data.
@RoToRa I was just using the terminology. Aside from your JSON not being wrapped in {} I can't see console.log(this) working since this is out of scope unless you are running it through a loop.
@tnt I understand that but this will not evaluate to anything, you need to change either the XML that PHP is using to output so that you have a better output or manually create your JSON string and output that instead.
0

I think your phone array is quite messy because you have:

phone[0] === {'@attributes':...}
phone[1] === '(123) 456 7890'
phone[2] === {'@attributes':...}

Is that what you realy wanted to have there?

3 Comments

the json comes from a php json_encode the origional xml is <phone type="cell" ext=""></phone> <phone type="work" ext="">(123) 456 7890</phone>
hmm, so there should be some validation because you couldn't be sure on which index is the phone number or the object literal (attributes), am I right?
only simple loop here in your next question: stackoverflow.com/questions/7203297/javascript-json-values/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.