1

I am serializing following class to JSON and returning through API:

public class HighStockChart
{
    public string name { get; set; }
    public IEnumerable<HighStockChartData> data { get; set; }
}

public class HighStockChartData
{
    public DateTime x { get; set; }
    public double y { get; set; }
}

Now when I am doing a console log for data , I am getting following format:

{x: "/Date(1519154100000)/", y: 392.61}
{x: "/Date(1519179300000)/", y: 392.732}
{x: "/Date(1519212600000)/", y: 392.732}
{x: "/Date(1519173000000)/", y: 392.733}
{x: "/Date(1519187400000)/", y: 392.757}
{x: "/Date(1519169400000)/", y: 392.761}

But I need the data without /Date. Example format below:

{x: 1519154100000), y: 392.61} {x: 1519179300000), y: 392.732}

Edit:

To build the date String I am doing ,

IEnumerable<HighStockChartData> stockChartXY = tag.Select(t => new HighStockChartData
                    {
                        x = DateTime.ParseExact(t.Date, "dd MMM yyyy HH:mm:ss:fff", null),
                        y = double.Parse(t.Value)
                    });

HistorianTagDetail historianTagDetailsresult = new HistorianTagDetail
                    {

                        Average = average,
                        HighStockChartData = stockChartXY
                    };

chartData = ChartHelper.CreateHighStockChart(tagDetails);

return Json(chartData);

By using jQuery to loop through and strip all data will work only if I am using q webClient. I have to find another solution , If I will try to send that as an email , using a service or use the chart in desktop application , then have to strip off the characters using C#.

Can I serialize the dates as Long (we have currently) but without "/Date()"?

Is there any custom attribute to set? Or Can I loop through object after serialization , remove the characters and then return?

ex:

chartData = ChartHelper.CreateHighStockChart(tagDetails);
var jsonData =  Json(chartData);  
var jDataList = new List<object>();


//Not actual code, just a theory I can think of 
foreach(jData in jsonData)
{
//Do something to strip of the brackets
//Add to a list 
jDataList.Add(jData);
}
return jDataList;
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • What json serializer are you using? Json.NET doesn't do that. – Sam Axe Mar 01 '18 at 04:43
  • I think I remember jQuery or one of its plugins formatting dates that way. – StriplingWarrior Mar 01 '18 at 04:44
  • Please include the code that you created this json string – Triet Pham Mar 01 '18 at 04:44
  • @SamAxe , I am just returning using , return Json(chartData); and Json method is in System.Web.MVC – Simsons Mar 01 '18 at 04:44
  • 1
    Or maybe it was an old version of ASP.NET MVC that did it this way. https://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx What version of MVC or Web API are you using, Simsons? – StriplingWarrior Mar 01 '18 at 04:51
  • You can override JsonResult methods and create json as per your need. Rather than overridng , I would recommend to use newtonsoft library and you can define your JsonSerialzerSetting where you can format such date – Akash KC Mar 01 '18 at 04:53
  • @SamAxe JSON.NET _can_ do that, if configured with `DateFormatHandling.MicrosoftDateFormat`, though by default it indeed does not, at least not in last versions. – Evk Mar 01 '18 at 04:58
  • @NightOwl888 that's wrong duplicate I think. OP wants to know how to _avoid_ serializing dates in this format, not how to read that format in javascript. – Evk Mar 01 '18 at 08:17

0 Answers0