2016-08-26 83 views
1

我無法獲取JSON對象中的JSON數組的項目。這裏是我的JSON是什麼樣子:

[ 
{ 
    "category":9, 
    "channels":[ 
     { 
      "id":5, 
      "title":"MYTITLE", 
      "active":true, 
      "recent":true, 
      "image":"/arts/736c1ad4-2dbe-40a6-859d-8dba89c26ec2.jpg", 
      "recent_tracks":{ 
       "vip":"https://api.xyzsite.com/recent_tracks/vip-3.json", 
       "free":"https://api.xyzsite.com/recent_tracks/free-3.json" 
      }, 
      "additional_vip_channels":[ 
       { 
        "channel_name":"vip-3a", 
        "recent_tracks_uri":"https://api.xyzsite.com/recent_tracks/vip-3a.json", 
        "streams":{ 
         "320":"http://streams.xyzsite.com/api/914/320/stream", 
         "64":"http://streams.xyzsite.com/api/914/64/stream", 
         "192":"http://streams.xyzsite.com/api/914/192/stream" 
        } 
       } 
      ], 
      "streams":{ 
       "free":"http://streams.xyzsite.com/api/31/56/stream", 
       "free_56":"http://streams.xyzsite.com/api/31/56/stream", 
       "free_128":"http://streams.xyzsite.com/api/31/128/stream", 
       "320":"http://streams.xyzsite.com/api/33/320/stream", 
       "64":"http://streams.xyzsite.com/api/33/64/stream", 
       "192":"http://streams.xyzsite.com/api/33/192/stream" 
      } 
     }, 

我用下面的代碼來獲取值:

$.getJSON('https://api.xyzsite.com/channels.json', function(response) { 
      $.each(response, function (index, value) { 
       var catId = value.category; 
       $.each(value.channels, function (index, value) {       
        XYZApp.channels.push({ 
         categoryId: catId, 
         id: value.id, 
         name: value.title, 
         image: value.image, 
         recent_tracks: { 
          vip: value.recent_tracks.vip, 
          free: value.recent_tracks.free 
         }, 
         streams: { 
          free_128: value.streams.free_128, 
          member_320: value.streams["320"], 
          member_64: value.streams["64"], 
          member_192: value.streams["192"] 
         } 
        }); 

        console.log(value.additional_vip_channels); 

       }); 

      }); 
     }). 
then ... 

我能得到的值,但我無法讀取。每個jQuery函數內additional_vip_channels陣列。我已經嘗試過:

  • value.additional_vip_channels.channel_name
  • value.additional_vip_channels [0] .channel_name
  • value.additional_vip_channels [ 「0」] CHANNEL_NAME

但他們沒有工作。 。

和日誌輸出也正在於此:

enter image description here

我怎樣才能在additional_vip_channels CHANNEL_NAME和其他數據?

+1

' value.additional_vip_channels [0] .channel_name'將是正確的方法。 –

+0

嘗試'value.channels [「0」]。additional_vip_channels [「0」]。channel_name' –

+0

日誌顯示值與預期一樣,那麼問題是什麼嘗試輸出'additional_vip_channels'的單個值。 – itzmukeshy7

回答

1

我相信,你會想:

channels[0]['additional_vip_channels'][0]['channel_name'] 

同樣的,這應該工作:(選擇的只是以不同的方式)

channels[0].additional_vip_channels[0].channel_name 

希望幫助和工程:)

+0

value.channels [0] .additional_vip_channels [0] .channel_name不起作用,我得到:Uncaught TypeError:無法讀取未定義的屬性'0' – burakk

+0

@burakk您是否嘗試過其他示例?我在手機上,所以無法測試自己,對不起。 –