0

In React native - I don't know how to get json data value that's key name contains a hyphen(-) like media-metadata

this is the API json response

 "approved_for_syndication": 1,
                "media-metadata": [
                    {
                        "url": "https://static01.nyt.com/images/2019/05/13/business/00deutschetrump2/merlin_154640208_f27c634b-ab02-42af-8c94-10d4b01d8ddf-square320.jpg",
                        "format": "square320",
                        "height": 320,
                        "width": 320
                    },
                    {
                        "url": "https://static01.nyt.com/images/2019/05/13/business/00deutschetrump2/merlin_154640208_f27c634b-ab02-42af-8c94-10d4b01d8ddf-thumbStandard.jpg",
                        "format": "Standard Thumbnail",
                        "height": 75,
                        "width": 75
                    },

and this is the code I have tried

  <Image source = {
      {
        uri: item.media-metadata[0].url,
        isStatic: true
      }
    }
    style = {
      {
        width: 100,
        height: 100
      }
    }
    />
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Possible duplicate of [How to get JSON objects value if its name contains dots?](https://stackoverflow.com/questions/2577172/how-to-get-json-objects-value-if-its-name-contains-dots) – Amadan May 21 '19 at 11:52
  • Also https://stackoverflow.com/questions/13869627/unable-to-access-json-property-with-dash , https://stackoverflow.com/questions/8758715/using-number-as-index-json , ... – Amadan May 21 '19 at 11:56
  • Try `item.['media-metadata'][0].url` ; '-' is being read as a special character here. Read this : https://stackoverflow.com/questions/5516106/are-dashes-allowed-in-javascript-property-names – Deepak May 21 '19 at 11:56
  • 1
    Possible duplicate of [Unable to access JSON property with "-" dash](https://stackoverflow.com/questions/13869627/unable-to-access-json-property-with-dash) – chazsolo May 21 '19 at 12:22

1 Answers1

2

Try

item["media-metadata"][0].url
Michael A. Schaffrath
  • 1,992
  • 1
  • 14
  • 23