0

I have a Restful WCF Http Service that returns JSON and works great, except the DateTime doesn't format correctly:

e.g. "AdjudDate": "/Date(1349409600000-0400)/"

After looking around I found an old post here but I could not get that code to work. It was last updated in 2011. Is there maybe a more current version or implementation? JSON.NET Serializer for WCF REST Services

Here's my code that returns an IEnumerable.

//All JSON values are good except for the dates:

private IEnumerable<Customer> ProcessWcfPost(Customer searchmodel)
{
    return _repository.GetCustomers(searchModel);
}

edit I've also of course tried changing the return type to string instead of IEnumerable as:

private string ProcessWcfPost(Customer searchmodel)
    {
        var result = _repository.GetCustomers(searchModel);
        return Newtonsoft.Json.JsonConvert.SerializeObject(result);
    }

But my JSON is not quite right: [{\"ClaimProcessId\":1599986,\"ClmHdrId\":1011,\"ClmLineId\":1011,\"ClmAdjId\":1011,\"ReceivedDtSur\":\"2012-12-05T00:00:00\",\"AdjudicationDtSur\":\"2011-10-05T00:00:00\"(on and on)...

Community
  • 1
  • 1
Rico
  • 1
  • 2
  • Have you considered using ASP.NET Web API? It makes REST services very simple to write, and the JSON serialization is built in using the Json() functions. – Jon Jul 25 '16 at 20:22
  • Yes sir, I wrote this initially as an ASP.NET Web API but I was ordered into make it into a HTTP WCF Service. Such is the business world :) – Rico Jul 25 '16 at 20:24
  • So using JsonConvert.SerializeObject() you're still getting the wrong date format? You can specify and attribute on your DateTime property to force how Json.Net formats it. Here is the guide on Json.Net date formatting, where you can see how to change it to the format you want: http://www.newtonsoft.com/json/help/html/datesinjson.htm – Jon Jul 25 '16 at 21:14
  • Thanks. The date is correct using JsonConvert.SerializeObject() but all of the JSON is loaded with extra backslashes and quotes. Am I missing a formatting option? I am using PostMan to call my HTTP WCF Services to render my JSON, maybe that's the issue? – Rico Jul 25 '16 at 21:38
  • The slashes are just escape characters for the " in C#. If you view a string in the C# debugger it will have those slashes. The actual string returned by your WCF service should be clean JSON. Can you post the full HTTP response here from PostMan? – Jon Jul 26 '16 at 13:48
  • Thanks @Mangist, here's the JSON "[{\"ClaimProcessId\":9996028,\"ClmToDtSur\":\"2015-10-02T00:00:00\",\"PatId\":99999,\"Fname\":\"Joe\",\"Lname\":\"Smith\,\"ClaimAmt\":18.2000}]" – Rico Jul 26 '16 at 15:08
  • @Rico I am facing the exact problem as yours, any news about how to solve it? Thanks – Mlle 116 Jul 03 '17 at 08:15

2 Answers2

0

I was facing the same problem and I could find a solution that might help anybody. This solution is based on DateTimeKind.Utc which will return date without -0400, ex:

Public Function test() As Date

Dim DateFormatted As New DateTime(2017, 07, 04, 8, 18, 42, DateTimeKind.Utc)
return DateFormatted 

End Function
Mlle 116
  • 1,149
  • 4
  • 20
  • 53
0

You can try use IsoDateTimeConverter

IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
string strJSON = JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented, timeConverter);
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45