Arrays 从MVC控制器返回Json数组

Arrays 从MVC控制器返回Json数组,arrays,json,asp.net-mvc,asp.net-mvc-4,Arrays,Json,Asp.net Mvc,Asp.net Mvc 4,我想为jwplayer返回如下Json数组: {"file":"example_file.mp4","large.file":"","hd.file":"","image":"http://www.example.com/example_image.jpg"} 但是,我可以返回数组属性名称中没有点(.)的数组。因为我的mvc viewmodel不能有。以可变的名称 {"file":"example_file.mp4","largefile":"","hdfile":"","image":"ht

我想为jwplayer返回如下Json数组:

{"file":"example_file.mp4","large.file":"","hd.file":"","image":"http://www.example.com/example_image.jpg"}
但是,我可以返回数组属性名称中没有点(.)的数组。因为我的mvc viewmodel不能有。以可变的名称

{"file":"example_file.mp4","largefile":"","hdfile":"","image":"http://www.example.com/example_image.jpg"}
控制器代码:

public ActionResult Index(int id)
        {
            return Json(_ext.GetSrcNow(id), JsonRequestBehavior.AllowGet);
        }
型号代码:

public SrcViewModel GetSrcNow(int Vid_id)
        {
            var mv = _ext.Get(p => p.video_id == Vid_id);
            if (mv == null) return null;
            return new SrcViewModel 
            {
                file = mv.vid_src_mp4,
                image = mv.vid_image,
                largefile = mv.vid_largesrc_mp4,
                hdfile = mv.vid_hdsrc_mp4
            };
        }
public class SrcViewModel
{
    public string file { get; set; }
    public string image { get; set; }
    public string largefile { get; set; }
    public string hdfile { get; set; }
}
视图模型代码:

public SrcViewModel GetSrcNow(int Vid_id)
        {
            var mv = _ext.Get(p => p.video_id == Vid_id);
            if (mv == null) return null;
            return new SrcViewModel 
            {
                file = mv.vid_src_mp4,
                image = mv.vid_image,
                largefile = mv.vid_largesrc_mp4,
                hdfile = mv.vid_hdsrc_mp4
            };
        }
public class SrcViewModel
{
    public string file { get; set; }
    public string image { get; set; }
    public string largefile { get; set; }
    public string hdfile { get; set; }
}
上面的代码与'largefile'和'hdfile'属性名配合得很好,但我希望它像'large.file'和'hd.file'一样


请帮我解决这个问题。谢谢

请尝试下面的代码片段

public class SrcViewModel
{
    public string file { get; set; }
    public string image { get; set; }
    public large large { get; set; }
    public hd hd { get; set; }
}

public class large
{
    public string file { get; set; }
}

public class hd
{
    public string file { get; set; }
}
。。。。。。

如果有任何问题,请告诉我

更新1:

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click here to get large.file</button>
<script>
function myFunction() {
    var test =JSON.parse('{"file":"example_file.mp4","image":"example_image.jpg","large":{"file":"Hello"},"hd":{"file":null}}');
    alert(test.large.file)
}
</script>

</body>
</html>

单击此处获取大文件
函数myFunction(){
var test=JSON.parse(“{”file:“example_file.mp4”,“image:“example_image.jpg”,“large:{”file:“Hello”},“hd:{”file:“null”}');
警报(test.large.file)
}
在从控制器返回之前,您可以使用创建JSON对象

下面是使用此库的基本示例

所需输出

{"First Name":"Prerak","PinCode":"32121A"}
序列化

JsonConvert.SerializeObject(new MyTest() { Name = "Prerak", PinCode= "32121A" });
名称
字段上的属性

public class MyTest
    {
        [JsonProperty(PropertyName = "First Name")]
        public string Name { get; set; }
        public string PinCode { get; set; }
    }

此外,请检查是否要使用基于json.net的自定义json序列化程序替换现有json序列化程序

视图模型代码:

public class SrcViewModel
{
    public string file { get; set; }

    [JsonProperty(PropertyName = "large.file")]
    public string largefile { get; set; }

    [JsonProperty(PropertyName = "hd.file")]
    public string hdfile { get; set; }

    public string image { get; set; }
}
public SrcViewModel GetSrcNow(int Vid_id)
    {
        var mv = _ext.Get(p => p.video_id == Vid_id);
        if (mv == null) return null;

         return new SrcViewModel 
         { 
            file = mv.vid_src_mp4,
            image = mv.vid_image,
            largefile = mv.vid_largesrc_mp4,
            hdfile = mv.vid_hdsrc_mp4
         };
    }
public ActionResult Index(int id)
        {
            var result = _ext.GetSrcNow(id);
            return Content(JsonConvert.SerializeObject(result), "application/json");
        }
型号代码:

public class SrcViewModel
{
    public string file { get; set; }

    [JsonProperty(PropertyName = "large.file")]
    public string largefile { get; set; }

    [JsonProperty(PropertyName = "hd.file")]
    public string hdfile { get; set; }

    public string image { get; set; }
}
public SrcViewModel GetSrcNow(int Vid_id)
    {
        var mv = _ext.Get(p => p.video_id == Vid_id);
        if (mv == null) return null;

         return new SrcViewModel 
         { 
            file = mv.vid_src_mp4,
            image = mv.vid_image,
            largefile = mv.vid_largesrc_mp4,
            hdfile = mv.vid_hdsrc_mp4
         };
    }
public ActionResult Index(int id)
        {
            var result = _ext.GetSrcNow(id);
            return Content(JsonConvert.SerializeObject(result), "application/json");
        }
控制器代码:

public class SrcViewModel
{
    public string file { get; set; }

    [JsonProperty(PropertyName = "large.file")]
    public string largefile { get; set; }

    [JsonProperty(PropertyName = "hd.file")]
    public string hdfile { get; set; }

    public string image { get; set; }
}
public SrcViewModel GetSrcNow(int Vid_id)
    {
        var mv = _ext.Get(p => p.video_id == Vid_id);
        if (mv == null) return null;

         return new SrcViewModel 
         { 
            file = mv.vid_src_mp4,
            image = mv.vid_image,
            largefile = mv.vid_largesrc_mp4,
            hdfile = mv.vid_hdsrc_mp4
         };
    }
public ActionResult Index(int id)
        {
            var result = _ext.GetSrcNow(id);
            return Content(JsonConvert.SerializeObject(result), "application/json");
        }

谢谢,返回的Json数组是
{“file”:“example_file.mp4”,“image”:“example_image.jpg”,“large”:{“file”:null},“hd”:{“file”:null}
但是我想把large.file和hd.file放在哪里[Serializable],我在Model、ViewModel或controller中?是的,在Jwplayer中使用,但首先我需要像上面一样使用controller重新运行Json数组,就像我在另一个网站上做的那样。。谢谢你想在javascript代码中获得“large.file”值,对吗?我已经更新了上面的代码片段。临时我在字段“large.file”中设置值仅用于测试目的。有关详细解决方案的任何人,请参阅