访问复杂的JSON对象

时间:2016-02-29 18:47:01

标签: javascript json

我有一个复杂的嵌套JSON,我试图在我的Javascript代码中解析/读取但不知道如何访问这些对象。以下是JSON响应:

{
"the-revenant-original-motion-picture-soundtrack": {
"uid": "fbde4e5c-e9f7-4d19-a1a3-8f3589ba1742",
"title": "The Revenant (Original Motion Picture Soundtrack)",
"genre": "Soundtrack",
"classical": false,
"tracks": [
  {
    "fileName": "19-cat-mouse.mp3",
    "title": "Cat & Mouse",
    "artists": [
      "Ryuichi Sakamoto, Alva Noto & Bryce Dessner"
    ],
    "genre": "Soundtrack",
    "album": "The Revenant (Original Motion Picture Soundtrack)",
    "codec": "mp3",
    "channels": "Stereo",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 19,
    "trackLength": 342,
    "diskNumber": 1
  },
  {
    "fileName": "20-the-revenant-main-theme-atmospheric.mp3",
    "title": "The Revenant Main Theme Atmospheric",
    "artists": [
      "Ryuichi Sakamoto"
    ],
    "genre": "Soundtrack",
    "album": "The Revenant (Original Motion Picture Soundtrack)",
    "codec": "mp3",
    "channels": "Stereo",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 20,
    "trackLength": 170,
    "diskNumber": 1
  },
  {
    "fileName": "21-final-fight.mp3",
    "title": "Final Fight",
    "artists": [
      "Ryuichi Sakamoto & Bryce Dessner"
    ],
    "genre": "Soundtrack",
    "album": "The Revenant (Original Motion Picture Soundtrack)",
    "codec": "mp3",
    "channels": "Stereo",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 21,
    "trackLength": 395,
    "diskNumber": 1
  }
]
},


"twilight-of-the-ghosts": {
"uid": "476fbe1a-1496-458e-9d3f-f11a8bffd74d",
"title": "Twilight of the Ghosts",
"classical": false,
"tracks": [
  {
    "fileName": "twilight-of-the-ghosts-twilight-of-the-ghosts-01-pinned-to-the-mattress.flac",
    "title": "Pinned to the Mattress",
    "artists": [
      "Twilight of the Ghosts"
    ],
    "album": "Twilight of the Ghosts",
    "isrc": "USVDE1000411",
    "codec": "FLAC 16 bits",
    "channels": "2",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 1,
    "trackLength": 274
  },
  {
    "fileName": "twilight-of-the-ghosts-twilight-of-the-ghosts-02-sinking-slowly-slowly-sinking.flac",
    "title": "Sinking Slowly Slowly Sinking",
    "artists": [
      "Twilight of the Ghosts"
    ],
    "album": "Twilight of the Ghosts",
    "isrc": "USVDE1000412",
    "codec": "FLAC 16 bits",
    "channels": "2",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 2,
    "trackLength": 270
  },
  {
    "fileName": "twilight-of-the-ghosts-twilight-of-the-ghosts-06-steamships-cross-the-desert.flac",
    "title": "Steamships Cross the Desert",
    "artists": [
      "Twilight of the Ghosts"
    ],
    "album": "Twilight of the Ghosts",
    "isrc": "USVDE1000416",
    "codec": "FLAC 16 bits",
    "channels": "2",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 6,
    "trackLength": 272
  },
  {
    "fileName": "twilight-of-the-ghosts-twilight-of-the-ghosts-07-time-tribe.flac",
    "title": "Time Tribe",
    "artists": [
      "Twilight of the Ghosts"
    ],
    "album": "Twilight of the Ghosts",
    "isrc": "USVDE1000417",
    "codec": "FLAC 16 bits",
    "channels": "2",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 7,
    "trackLength": 378
  },
  {
    "fileName": "twilight-of-the-ghosts-twilight-of-the-ghosts-08-how-did-they-find-me.flac",
    "title": "How Did they Find Me?",
    "artists": [
      "Twilight of the Ghosts"
    ],
    "album": "Twilight of the Ghosts",
    "isrc": "USVDE1000418",
    "codec": "FLAC 16 bits",
    "channels": "2",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 8,
    "trackLength": 290
  },
  {
    "fileName": "twilight-of-the-ghosts-twilight-of-the-ghosts-09-search-for-the-way-home.flac",
    "title": "Search For The Way Home",
    "artists": [
      "Twilight of the Ghosts"
    ],
    "album": "Twilight of the Ghosts",
    "isrc": "USVDE1000419",
    "codec": "FLAC 16 bits",
    "channels": "2",
    "bitsPerSample": 16,
    "samplingRate": 44100,
    "sequenceNumber": 9,
    "trackLength": 281
   }
  ]
 }
}

我想访问" the-revenant-original ......"并展示它和"暮光之城......"以及主对象中的任何其他数据。并且还可以访问"曲目"与每个对象关联的数据。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

将其粘贴在变量中

var example = { "jsonresponse": { ... } };

然后您可以通过以下方式访问它:

example.jsonresponse;

example["jsonresponse"];

您可能还想查看JSON.parse()和JSON.stringify()https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON