Chris Johnson
Chris Johnson

Reputation: 1340

How can I address JSON properties with complex names in Javascript?

Given a JSON object such as:

{
    "@abc.def":"foo"
}

How do you address a property named like that?

e.g. this doesn't work

var x = [email protected]

Upvotes: 1

Views: 237

Answers (3)

user5786953
user5786953

Reputation: 43

It will be obj["@abc.def"] in your code.

Upvotes: 1

Ted A.
Ted A.

Reputation: 2302

As @abc.def is not a valid JavaScript identifier, accessing a property with this name must use bracket notation.

The working code would be: var x = obj['@abc.def']

Upvotes: 3

skypjack
skypjack

Reputation: 50550

You can use: var x = obj["@abc.def"].
Of course, I suppose that obj is defined as: var obj = { "@abc.def":"foo" }.

Upvotes: 0

Related Questions