0

I was trying to iterate following JSON object. when i try to access the content-items using page.content-items getting error.Is it not possible to access an object which have a key with "-"?.Please give a solution how to access this object.

{
      "page": {
        "title": "Romantic Comedy",
        "total-content-items" : "54",
        "page-num-requested" : "1",
        "page-size-requested" : "20",
        "page-size-returned" : "20",
        "content-items": {
          "content": [
            {
              "name": "The Birds",
              "poster-image": "poster1.jpg"
            },
            {
              "name": "Rear Window",
              "poster-image": "poster2.jpg"
            },
            {
              "name": "Family Pot",
              "poster-image": "poster3.jpg"
            }
          ]
        }
      }
    }
Alex Varghese
  • 2,000
  • 16
  • 17

3 Answers3

1

It is possible to access the key with the - character, but not using dot notation. You need to use brakcet notation:

page['content-items']

to access the property.

War10ck
  • 12,387
  • 7
  • 41
  • 54
1

Just use string value like in arrays:

page['content-items']
hsz
  • 148,279
  • 62
  • 259
  • 315
1

Use page['content-items'] because content-item is not a valid JavaScript identifier.

vahissan
  • 2,322
  • 16
  • 26