2016-02-29 8 views
-1

У меня есть сложный вложенный JSON, который я пытаюсь разобрать/прочитать в моем Javascript-коде, но не знаю, как получить доступ к объектам. Ниже приводится ответ JSON:Доступ к сложным объектам 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 
    } 
    ] 
} 
} 

Я хочу, чтобы получить доступ к «в-Потустороннему-оригиналу ...» и отобразить его и «сумерки ...» и любые другие данные, в основном объекте в первую очередь. А также получить доступ к данным «дорожки», связанным с каждым объектом. Какие-либо предложения ?

+0

ОК? Что вас беспокоит? – Mathletics

+1

[Вот как получить доступ к свойствам] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors) и [здесь, как прокручивать ключи объекта.] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) Теперь у вас есть все инструменты для выполнения этой работы. –

+0

@Mathletics: Предположим, что основным объектом являются данные {....} , поэтому доступ к данным (0) или данным (1) не даст мне «реванш» или «сумерки ..» и не уверен, как доступ к ним. – noobcoder

ответ

1

Палка его в переменной

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

И тогда вы можете получить доступ к нему через:

example.jsonresponse; 

или

example["jsonresponse"]; 

Вы также можете проверить JSON.parse () и JSON.stringify() https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON

+0

Спасибо. Это то, что я искал. – noobcoder