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

3 Answers 3

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");
        }
Sign up to request clarification or add additional context in comments.

Comments

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>

6 Comments

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
where to put [Serializable], I am in Model, ViewModel or controller?
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.. thanks
You want to get "large.file" value in javascript code, right ?
I have updated my above code snippet. Temporary i have set value in field "large.file" only for testing purpose.
|
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

1 Comment

for anyone looking for a detailed solution please see stackoverflow.com/a/23963699/22858

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.