分配从JSON获得的变量

时间:2017-06-30 14:12:21

标签: jquery html json variables variable-assignment

我是瞎了吗?为什么我不能从这里得到我的变量'progress'的值? 它没有保存为字符串,'obj.songProgress [i] .songProgress'打印到控制台,但它自己的进度不会。进一步增加的变量将被分配好。

  $(document).on("click",".profTemp", function()
            {    

              if(!$(this).hasClass('selected'))
              {

                var selectedUser;
                var previous = null;
                var current = null;
                var assigned = false;
                var currentSong;
                var previousSong;
                var previousUser = null;

                selectedUser = $(this).children('span').text();
                selectedUser = selectedUser.trim();

                $('.profTemp').removeClass('selected');
                clearInterval(pollUser);

                $(this).toggleClass('selected');

                pollUser = setInterval(function()
                {
                  console.log(selectedUser);
                  $.ajax({
                    url: '/getJSON',
                    dataType: 'json',
                    success: function(response) {
                      obj = JSON.parse(response);
                      var changeTo;
                      var deviceID;
                      var playing;
                      var progress;


                        for(var i = 0; i < obj.clients.length; i++)
                        {
                          if(selectedUser == obj.clients[i].userName) 
                          {
                            console.log(selectedUser + " inside if");
                            current = JSON.stringify(obj.clients[i]);
                            changeTo = obj.clients[i].uri;
                            device = obj.clients[i].deviceID
                            playing = obj.clients[i].isPlaying;
                            currentSong = obj.clients[i].songName;                                     
                          }
                        }

                        for(var i = 0; i < obj.songProgress.length; i++)
                          {
                            if(selectedUser == obj.songProgress[i].userName)
                            {
                              progress = obj.songProgress[i].songProgress;
                              console.log(obj.songProgress[i].songProgress + progress);
                            }                              
                          } 

这是JSON:

    {
   "clients":[
      {
         "userName":"test1 test1",
         "albumArt":"https://i.scdn.co/image/e0f8a9160f4eaf4bee5816ca6f7833acbabf67ce",
         "songName":"Everyday",
         "artistName":"Angie Stone",
         "albumName":"Black Diamond",
         "songLength":1498831947299,
         "isPlaying":false,
         "uri":"spotify:track:7okKZ5JfRIJBcqKhX2fTmU",
         "deviceID":"0a00d5d75dc4809ed68b92b4d998e71f36a69041",
         "trackID":"7okKZ5JfRIJBcqKhX2fTmU",
         "songProgress":101580
      },
      {
         "userName":"testaccount",
         "albumArt":"https://i.scdn.co/image/1aa6bb570db21d1b5edd3071e39c8ac0530dd99d",
         "songName":"Tyrant (feat. Jorja Smith)",
         "artistName":"Kali Uchis",
         "albumName":"Tyrant (feat. Jorja Smith)",
         "songLength":1498832434724,
         "isPlaying":true,
         "uri":"spotify:track:6u24cRDn2mTKcXxxtgmIsV",
         "deviceID":"2198676338ce85949579e2d6350962b6552bbbf5",
         "trackID":"6u24cRDn2mTKcXxxtgmIsV",
         "songProgress":1501
      }
   ],
   "songProgress":[
      {
         "userName":"test1 test1",
         "songProgress":101580
      },
      {
         "userName":"testaccount",
         "songProgress":1501
      }
   ]
}

1 个答案:

答案 0 :(得分:1)

console.log(obj.songProgress [i] .songProgress + progress); 上面的状态是添加数据并为您提供总结数据。

检查小提琴中的答案https://jsfiddle.net/hnofbq8m/

if(selectedUser == obj.songProgress[i].userName)
  {
    progress = obj.songProgress[i].songProgress;
    console.log(obj.songProgress[i].songProgress, progress);
  }