2017-01-08 30 views
0

对不起,我只想说清楚我在问什么。如何将JSON转换为MVC中的Jquery数组ASp.Net

我在我的控制器中的JSON结果: -

public JsonResult Top7Video() 
    { 
     db.Configuration.ProxyCreationEnabled = false; 
     var result = (from v in db.TblVideos 
         orderby v.FileName 
         select new { title= v.FileName, artist=v.Artist,Image=v.Image,ID=v.ID}).Take(7).ToList(); 

     return Json(result,JsonRequestBehavior.AllowGet); 
    } 

而在我的Razor视图我想把它当作一个jQuery数组。我尝试以下方法: -

<script type="text/jscript"> 
 

 
     var myarray = '{"title": "3. Ellie-Goulding","artist": "","mp4": "~/video/Ellie-Goulding.mp4","ogv": "~/video/Ellie-Goulding.ogv","webmv": "~/video/Ellie-Goulding.webm","poster": "~/video/VideoImg/play1.png"}'; 
 
     //var myarray = '{"Err0":"Only letters and white space allowed in Name"}'; 
 
     
 
     //alert(myarray); 
 

 
     $(function() { 
 
      $.ajax({ 
 
       type: "GET", 
 
       url: "/Home/Top7Video", 
 
       data:"{}", 
 
       datatype: "Json", 
 
       success: function (data) { 
 
        $.each(arr, function (index, value) { 
 
         myarray.push([',{ "title": "' + value.FileName + '", "artist": "' + value.Artists + '", "mp4": "~/video/' + value.FileName + '", "ogv": "~/video/' + value.FileName + '", "webmv": "~/video/' + value.FileName + '", "poster": "~/video/VideoImg/' + value.Image + '" }']);      
 
        }); 
 
       } 
 
      }); 
 
     }); 
 

 

 
     var arr = $.parseJSON(myarray); //convert to javascript array 
 
     alert(arr); 
 

 

 
     $(function() { 
 
      $.ajax({ 
 
       type: "GET", 
 
       url: "/Home/Top7Video", 
 
       datatype: "Json", 
 
       success: function (data) { 
 
        $.each(data, function (index, value) { 
 
         $('#dropdownVideo').append('<li><div><a href="javascript:;" style="display: none;">×</a><a href="javascript:;" class="jp-playlist-item jp-playlist-current" tabindex="0">' + value.FileName + '<span class="jp-artist"> Song by ' + value.Artist + '</span></a></div></li>'); 
 
        }); 
 

 
       } 
 
      }); 
 
     }); 
 

 

 
     
 

 

 
     $(document).ready(function() { 
 

 
      new jPlayerPlaylist({ 
 
       jPlayer: "#jquery_jplayer_1", 
 
       cssSelectorAncestor: "#jp_container_1" 
 
      }, 
 
       arr, 
 
       { 
 
        swfPath: "../../dist/jplayer", 
 
        supplied: "webmv,ogv,mp4", 
 
        useStateClassSkin: true, 
 
        autoBlur: false, 
 
        smoothPlayBar: true, 
 
        keyEnabled: true 
 
       }); 
 

 
     }); 
 

 

 

 
    </script> 
 

 
Following is my HTML:-
<div class="video-main"> 
 
      <div class="video-record-list"> 
 
       <div id="jp_container_1" class="jp-video jp-video-270p" role="application" aria-label="media player"> 
 
        <div class="jp-type-playlist"> 
 
         <div id="jquery_jplayer_1" class="jp-jplayer" style="width: 480px; height: 270px;"> 
 
          <img id="jp_poster_0" src="~/video/VideoImg/play2.png" style="width: 480px; height: 270px; display: inline;"> 
 
          <video id="jp_video_0" preload="metadata" src="~/video/Ellie-Goulding.mp4" title="1. Ellie-Goulding" style="width: 0px; height: 0px;"></video> 
 
         </div> 
 
         <div class="jp-gui"> 
 
          <div class="jp-video-play" style="display: block;"> 
 
           <button class="jp-video-play-icon" role="button" tabindex="0">play</button> 
 
          </div> 
 
          <div class="jp-interface"> 
 
           <div class="jp-progress"> 
 
            <div class="jp-seek-bar" style="width: 100%;"> 
 
             <div class="jp-play-bar" style="width: 0%;"></div> 
 
            </div> 
 
           </div> 
 
           <div class="jp-current-time" role="timer" aria-label="time">00:00</div> 
 
           <div class="jp-duration" role="timer" aria-label="duration">00:30</div> 
 
           <div class="jp-controls-holder"> 
 
            <div class="jp-controls"> 
 
             <button class="jp-previous" role="button" tabindex="0">previous</button> 
 
             <button class="jp-play" role="button" tabindex="0">play</button> 
 
             <button class="jp-next" role="button" tabindex="0">next</button> 
 
            </div> 
 
            <div class="jp-volume-controls"> 
 
             <button class="jp-mute" role="button" tabindex="0">mute</button> 
 
             <button class="jp-volume-max" role="button" tabindex="0">max volume</button> 
 
             <div class="jp-volume-bar"> 
 
              <div class="jp-volume-bar-value" style="width: 100%;"></div> 
 
             </div> 
 
            </div> 
 
            <div class="jp-toggles"> 
 

 
             <button class="jp-full-screen" role="button" tabindex="0">full screen</button> 
 
            </div> 
 
           </div> 
 
           <div class="jp-details" style="display: none;"> 
 
            <div class="jp-title" aria-label="title">1. Ellie-Goulding</div> 
 
           </div> 
 
          </div> 
 
         </div> 
 
         <div class="jp-playlist"> 
 
          <ul id="dropdownVideo" style="display: block;"> 
 
           <li class="jp-playlist-current"> 
 
            <div> 
 
             <a href="javascript:;" class="jp-playlist-item-remove" style="display: none;">×</a> 
 
             <a href="javascript:;" class="jp-playlist-item jp-playlist-current" tabindex="0"> 
 
              1. Ellie-Goulding 
 
              <span class="jp-artist">by Pixar</span> 
 
             </a> 
 
            </div> 
 
           </li> 
 
           
 
          </ul> 
 
         </div> 
 
         <div class="jp-no-solution" style="display: none;"> 
 
          <span>Update Required</span> 
 
          To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>. 
 
         </div> 
 
        </div> 
 
       </div> 
 
      </div> 
 
     </div>

我不能到JSON转换为jQuery的阿雷。我得到以下错误: -

遗漏的类型错误:myarray.push不是一个函数

我是新进JSON和这里搜查了很多关于解决How to response an array in json to jquery?How to convert Json object to Jquery Array?。但我无法解决这个问题。请帮忙!!

+2

'myarray'是一个字符串,而不是一个数组。也许你打算使用括号'[]'而不是引号''''? – David

+1

'data'已经是一个数组。如果你想要这些属性名称('标题','艺术家'等),然后修改你的控制器发送一个具有这些属性名称的匿名对象的集合 - “select new {title = v.FileName,....}' –

+0

@David使用[]代替''也没有帮助。我得到相同的错误信息和警报(arr);我得到[对象] [对象] – Deepak

回答

0

添加这些行

myarray = JSON.parse(myarray); 
myarray = [myarray]; 

此行

var myarray = '{"title": "3. Ellie-Goulding","artist": "","mp4": "~/video/Ellie-Goulding.mp4","ogv": "~/video/Ellie-Goulding.ogv","webmv": "~/video/Ellie-Goulding.webm","poster": "~/video/VideoImg/play1.png"}'; 

myArray的是一个字符串下。你不能推动一个字符串。

+0

这样做我得到错误: - 未捕获SyntaxError:在位置1的JSON中意外的标记o – Deepak

0

正如其他人的评论都提到,下面的线是不是数组

var myarray = '{"title": "3. Ellie-Goulding","artist": "","mp4": "~/video/Ellie-Goulding.mp4","ogv": "~/video/Ellie-Goulding.ogv","webmv": "~/video/Ellie-Goulding.webm","poster": "~/video/VideoImg/play1.png"}'; 

但一个字符串变量!你不能打电话给push方法。所以首先要做的是将其设置为适当的数组。你的action方法返回一个集合的json数据。因此,只需使用$ .each循环即可。并根据需要从每个项目创建一个新的js对象并推送到数组。另外,就像斯蒂芬在评论中提到的那样,你可能会考虑使用action方法本身的变换值发送数组,而不是在你的javascript中进行字符串连接!

我还看到您正在进行2个调用,一个用于填充数组,另一个用于将项添加到下拉列表中。既然它与你获得的数据相同,为什么不能在同一个Ajax调用的成功回调函数中执行这两个操作?

+0

是的,我想我正在取得一些进展,通过该代码,我将两个调用合并为一个json,我的#dropdownVideo获取了所需的详细信息,但没有获得数组。弹出,我得到了错误: - 未捕获的SyntaxError:意外的JSON输入结束。请我真的很感谢帮助。 – Deepak

+0

数组现在看起来像: - va r item = { title:value.FileName, artist:value.Artist, mp4:“〜/ video /”+ value.FileName, ogv:“〜/ video /”+ value.FileName, webmv:“ 〜/ video /“+ value.FileName, poster:”〜/ video/VideoImg /“+ value.Image //待办事项:在此处添加其他属性以及 }; myarray.push(item); – Deepak