从MVC控制器返回Json数组

时间:2014-05-30 11:34:57

标签: 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":"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
            };
        }

ViewModel代码:

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'

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

3 个答案:

答案 0 :(得分:1)

请尝试使用以下代码段。

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; }
}

...... ......

{
    file = mv.vid_src_mp4,
    image = mv.vid_image,
    large = new large() { file = mv.vid_largesrc_mp4 },
    hd = new hd() { file =  mv.vid_hdsrc_mp4 }
}

如果有任何疑虑,请告诉我。

更新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>

答案 1 :(得分:1)

[已解决] 最后使用Json.net

实现了这一目标

ViewModel代码:

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");
        }

答案 2 :(得分:0)

也许您可以在从控制器返回之前使用Json.NET来创建JSON对象。

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

所需的输出

{"First Name":"Prerak","PinCode":"32121A"}

<强>序列化

JsonConvert.SerializeObject(new MyTest() { Name = "Prerak", PinCode= "32121A" });

Name字段

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

此外,请检查Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible?以防您想要使用基于JSON.net的自定义json序列化程序替换现有的json序列化程序