mart
mart

Reputation: 101

refer to an element of JSON (Javascript) object

How do I refer to an element of JSON (Javascript) object. exampe: alert(homes.Agents[1].name);

<script>
    var homes = [
 {
    "Agents" : {
        "name" : "Bob Barker",
        "name" : "Mona Mayflower" 
    },
    "Listings" : [
        {
            "h_id": "3",
            "city": "Dallas",
            "state": "TX",
            "zip": "75201",
            "price": "162500" 
        },
        {
            "h_id": "4",
            "city": "Bevery Hills",
            "state": "CA",
            "zip": "90210",
            "price": "319250" 
        },
        {
            "h_id": "5",
            "city": "New York",
            "state": "NY",
            "zip": "00010",
            "price": "962500" 
        } 
    ]
}

];

</script> 

Upvotes: 7

Views: 49376

Answers (6)

Brett Widmeier
Brett Widmeier

Reputation: 1077

Example taken from the following link:

var myJSONObject = {"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

"myJSONObject.bindings[0].method" will return "newURI"

Upvotes: 0

Jordan Running
Jordan Running

Reputation: 106027

Your JSON syntax is wrong. You can't have the same key twice in an object. Instead, you need an array:

var homes = {
  "Agents" : [
    { "name" : "Bob Barker" },
    { "name" : "Mona Mayflower" }
  ],
  ...
}

Then you can access the agents like so:

homes.Agents[1] // => { "name": "Mona Mayflower" }

// or

homes.Agents[1].name // => "Mona Mayflower"

Upvotes: 6

Crast
Crast

Reputation: 16326

That's not exactly very good JSON above there, in the case of the Agents value the second key will override the first.

You probably meant:

 "Agents" : [
     {"name" : "Bob Barker"},
     {"name" : "Mona Mayflower"} 
 ],

Then you'd access the first agent's name as

homes[0]['Agents'][0]['Name']

Similarly, to get one of the values from the Listings, you'd do something akin to:

homes[0]['Listings'][0]['city']
- or -
homes[0].Listings[0].city

The dot syntax can be used wherever there is a valid identifier, else you need to use the array syntax.

As a side note, I'm not sure the structure of the data, but it's possible that you can eliminate the outer-level [] that's enclosing your whole structure in an array. Then you wouldn't need to access everything as homes[0]['Listings'] and instead simply homes['Listings'].

Upvotes: 14

sberry
sberry

Reputation: 132018

Homes is an Array, so your first accessor is index based.

homes[0]

Agents is an Object, and Object containing two keys of the same name. That is a no-no.

If you are defining this data yourself, you should change

"Agents": {
    "name" : "Bob Barker",
    "name" : "Mona Mayflower"
}

to

 "Agents": [
        {"name" : "Bob Barker"},
        {"name" : "Mona Mayflower"}
    ]

Then you could access the data in question by

homes[0].Agents[1].name

Upvotes: 3

gtd
gtd

Reputation: 17246

This is invalid JSON Agents is just an object, not an array. If it was restructured like this:

"Agents" : [
  { "name" : "Bob Barker"},
  { "name" : "Mona Mayflower"}]

Then you could use homes.Agents[1].name to get 'Mona Mayflower'

Upvotes: 1

Derek Swingley
Derek Swingley

Reputation: 8752

You should give your object properties unique names. In your example, the second name property is overwriting the first so that homes[0].Agents.name will always be "Mona Mayflower".

Once you get that sorted, I think you're looking for this:

homes[0].Agents.name 

Upvotes: 2

Related Questions