1

Quick question.

I have a json file:

{
   "RentalCore": {
                "@PickUpDate": "2014-09-22T10:00:00Z",
                "@ReturnDateT": "2014-10-06T10:00:00Z",
                "Location": {
                    "@Name": "NY - Airport"
                },
                "ReturnLocation": {
                    "@Name": "NY - Airport"
                }
            }
}  

Normally if you have an object you'd access its objects with . notation.

But I can't access @PickUpDate etc. Elements that contain '@'.

Could someone shed me some light on this?

get error : Uncaught SyntaxError: Invalid or unexpected token

if I try and use dot notation to access @PickUpDate

1

2 Answers 2

4

You need to access via bracket [] syntax. In JS, if you can't access property with . syntax, do it via brackets and pass property name as a string.

var obj = {
   "RentalCore": {
                "@PickUpDate": "2014-09-22T10:00:00Z",
                "@ReturnDateT": "2014-10-06T10:00:00Z",
                "Location": {
                    "@Name": "NY - Airport"
                },
                "ReturnLocation": {
                    "@Name": "NY - Airport"
                }
            }
} 

console.log(obj.RentalCore['@PickUpDate']);

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

Comments

0

Use [] to access the object properties value which can not be accessed using dot(.) notation.

DEMO

var jsonObj = {
    "RentalCore": {
        "@PickUpDate": "2014-09-22T10:00:00Z",
        "@ReturnDateT": "2014-10-06T10:00:00Z",
        "Location": {
            "@Name": "NY - Airport"
        },
        "ReturnLocation": {
            "@Name": "NY - Airport"
        }
    }
} 

console.log(jsonObj.RentalCore['@ReturnDateT']);

Comments

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.