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

TSlegaitis
  • 1,231
  • 15
  • 29
  • 2
    Possible duplicate of [JavaScript property access: dot notation vs. brackets?](http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – str Mar 23 '17 at 11:57

2 Answers2

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']);
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
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']);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123