0

I want to return Json array like this for jwplayer:

{"file":"example_file.mp4","large.file":"","hd.file":"","image":"http://www.example.com/example_image.jpg"}

but however I can just return array without dot (.) in array attribute names. because my mvc viewmodel can't have . in the varible name.

{"file":"example_file.mp4","largefile":"","hdfile":"","image":"http://www.example.com/example_image.jpg"}

Controller code:

public ActionResult Index(int id)
        {
            return Json(_ext.GetSrcNow(id), JsonRequestBehavior.AllowGet);
        }

Model code:

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 code:

public class SrcViewModel
{
    public string file { get; set; }
    public string image { get; set; }
    public string largefile { get; set; }
    public string hdfile { get; set; }
}

The above code is working perfect with 'largefile' and 'hdfile' attribute names, but I want it like 'large.file' and 'hd.file'

Please help me solve this issue. Thanks

nisarg parekh
  • 413
  • 4
  • 23
aadi1295
  • 982
  • 3
  • 19
  • 47

3 Answers3

2

[Solved] Finally achieved this by using Json.net

ViewModel code:

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

Model Code:

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

Controller Code:

public ActionResult Index(int id)
        {
            var result = _ext.GetSrcNow(id);
            return Content(JsonConvert.SerializeObject(result), "application/json");
        }
aadi1295
  • 982
  • 3
  • 19
  • 47
1

Please try with the below code snippet.

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

Let me know if any concern.

UPDATE 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>
Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50
  • Thanks, return Json array is `{"file":"example_file.mp4","image":"example_image.jpg","large":{"file":null},"hd":{"file":null}}` but I want large.file and hd.file – aadi1295 May 30 '14 at 12:15
  • where to put [Serializable], I am in Model, ViewModel or controller? – aadi1295 May 30 '14 at 12:17
  • Yes, to use in Jwplayer, but first I need to retrun json array like above using controllor, like I have done this on my other website [click here](http://cpusort.com/search/2600k).. thanks – aadi1295 May 30 '14 at 12:25
  • You want to get "large.file" value in javascript code, right ? – Jayesh Goyani May 30 '14 at 12:29
  • I have updated my above code snippet. Temporary i have set value in field "large.file" only for testing purpose. – Jayesh Goyani May 30 '14 at 12:46
  • actually first I thought that javascript will good with jwplayer, but I need to send this Json array url to plugin attribute of jwplayer. Thats why I don't need it in javascript now. I hope you can suggest a way around to do that in Model. thanks for your time – aadi1295 May 30 '14 at 13:24
0

Perhaps you can use Json.NET to create your JSON object before returning from the controller.

Following is a basic example to use this library

Desired output

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

Serialization

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

Attribute over the Name field

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

Also, Please check Using JSON.NET as the default JSON serializer in ASP.NET MVC 3 - is it possible? in case you want to replace the existing json serializer with a customised one based on JSON.net

Community
  • 1
  • 1
Prerak K
  • 10,940
  • 7
  • 30
  • 37