简体   繁体   中英

Javascript: Parse array of json objects within a string

I have an array of json object within a string:

[
  {
    "title": "Ham on Rye",
    "alternativeTitles": [],
    "secondaryYearSourceId": 0,
    "sortTitle": "ham on rye",
    "sizeOnDisk": 0,
    "status": "released",
    "overview": "A bizarre rite of passage at the local deli determines the fate of a generation of teenagers, leading some to escape their suburban town and dooming others to remain…",
    "inCinemas": "2019-08-10T00:00:00Z",
    "images": [
      {
        "coverType": "poster",
        "url": "http://image.tmdb.org/t/p/original/px7iCT1SgsOOSAXaqHwP50o0jAI.jpg"
      }
    ],
    "downloaded": false,
    "remotePoster": "http://image.tmdb.org/t/p/original/px7iCT1SgsOOSAXaqHwP50o0jAI.jpg",
    "year": 2019,
    "hasFile": false,
    "profileId": 0,
    "pathState": "dynamic",
    "monitored": false,
    "minimumAvailability": "tba",
    "isAvailable": true,
    "folderName": "",
    "runtime": 0,
    "tmdbId": 527176,
    "titleSlug": "ham-on-rye-527176",
    "genres": [],
    "tags": [],
    "added": "0001-01-01T00:00:00Z",
    "ratings": {
      "votes": 5,
      "value": 6.9
    },
    "qualityProfileId": 0
  }
]

When I try to parse my array of json objects, it always returns a string, so when I try to access it by:

let data = JSON.parse(JSON.stringify(jsonData));

console.log(data[0].title)

it returns a specific character like "[", as if it was a string.

Te problem is that you are stringify ing something that is already a JSON string.
Change this:

let data = JSON.parse(JSON.stringify(jsonData));

to this:

let data = JSON.parse(jsonData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM