Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
output required

result = [Object, Object, Object, Object, Object, Object, Object]

output coming

result = "[{"label":"11-03-2016","value":"159","year":"2000"},{"label":"12-03-2016","value":"3207","year":"2001"},{"label":"13-03-2016","value":"4610","year":"2002"},{"label":"14-03-2016","value":"3418","year":"2003"},{"label":"15-03-2016","value":"551","year":"2004"}]"

What I have tried:

C#
public JsonResult GraphData()
        {
            GraphModel oGraphModel = null;
            List<graphmodel> lstGraphModel = new List<graphmodel>();
            int index = 0;
            Random random = new Random();
            while (index < 5)
            {
                oGraphModel = new GraphModel();
                oGraphModel.label = DateTime.Now.AddDays(index).ToString("dd-MM-yyyy");
                oGraphModel.value = random.Next(index, 5000).ToString();
                oGraphModel.year = (index + 2000).ToString();
                lstGraphModel.Add(oGraphModel);
                index++;
            }
            var jsonSerializer = new JavaScriptSerializer();
            string data = jsonSerializer.Serialize(lstGraphModel);            
            return Json(data, JsonRequestBehavior.AllowGet);
        }


 <script type="text/javascript">
        $(document).ready(function() {
           new Morris.Line({
                element: 'line-chart',
                data: Graph(),
                xkey: 'year',
                ykeys: ['value'],
                async: true,
                labels: ['value'],
                lineColors: ['#D9534F'],
                lineWidth: '2px',
                hideHover: true
           });

           new Morris.Area({
               element: 'area-chart',
               data: Graph(),
               xkey: 'year',
               ykeys: ['value'],
               async: true,
               labels: ['value'],
               lineColors: ['#428BCA'],
               lineWidth: '2px',
               hideHover: true
           });

        });

        function Graph() {
            var data = "";
            $.ajax({
                type: 'GET',
                url: '@Url.Action("GraphData", "Home")',
                dataType: 'json',
                async: true,
                contentType: "application/json; charset=utf-8",
                data: {},
                success: function (result) {
                    data = result;
                },
                error: function (xhr, status, error) {
                    alert(error);
                }
            });
            return data;
        }
    </script>
Posted
Updated 10-Mar-16 22:01pm
v2
Comments
lukeer 11-Mar-16 3:10am    
Please, when posting code, wrap it in tags:
<pre lang="c#">YourCodeHere();</pre>
That's a lot more readable.
MayankSemwal 11-Mar-16 3:13am    
Ok i will keep in mind from next time.

1 solution

Your "output desired" cannot be turned back into data as the details of the object will go missing.

Serialisation has to go down to the lowest level of the object graph (the properties as string, date or number) in order to then recreate that object graph.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900