简体   繁体   English

Javascript:解析字符串中 json 对象的数组

[英]Javascript: Parse array of json objects within a string

I have an array of json object within a string:我在一个字符串中有一个 json object 数组:

[
  {
    "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:当我尝试解析我的 json 对象数组时,它总是返回一个字符串,所以当我尝试通过以下方式访问它时:

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.问题是您正在对已经是 JSON stringify的东西进行字符串化。
Change this:改变这个:

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

to this:对此:

let data = JSON.parse(jsonData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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