1

I have two projects with one is a server and the other one is the caller. my server is returning an object from its function, and when the caller receive the result, it is receiving the data as a json.

Class

public class MyClass
{
    public int myInt{ get; set; }
    public string myString{ get; set; }
    public DateTime myDate{ get; set; }
}

Server Side

[HttpPost]
public async Task<ActionResult> Index()
{
     var tmpResult = await . . .
     return new MyClass();
}

Caller Side

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
Stream dataStream = webRequest.GetRequestStream();
WebResponse response = webRequest.GetResponse();
dataStream = response.GetResponseStream();
using (StreamReader reader = new StreamReader(dataStream))
{
     responseFromServer = reader.ReadToEnd();
     reader.Close();
}

currently, my response is like :

{"myInt":0,"myString":"a","myDate":"/Date(1531908040342)/"}

What I want to achieve would be like :

{"myInt":0,"myString":"a","myDate":"2018-07-18 00:00:00.000"}

I thought that by adding JsonConverter to my class, it would solve my problem.

public class MyClass
{
    public int myInt{ get; set; }
    public string myString{ get; set; }

    [JsonConverter(typeof(CustomDateTimeConverter))]
    public DateTime myDate{ get; set; }
}

class CustomDateTimeConverter : IsoDateTimeConverter
{
    public CustomDateTimeConverter()
    {
        base.DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff";
    }
}

But, the response that I get was still the same. I wonder how should I change my model to make it happen? or should I convert my response into object, then re-serialize again?

*Addition : I hope that I dont need to type anything like DateTime.ToString(). Because this way, I need convert my response to object, then type it 1 by 1. What I hope that I could get, is the response from server, already convert it to ISO format rather than Json Format

Mark
  • 2,041
  • 2
  • 18
  • 35
  • Possible duplicate of [Given a DateTime object, how do I get an ISO 8601 date in string format?](https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format) – Nitin Sawant Jul 18 '18 at 10:15
  • @NitinSawant this would be the worst case, so that I need to type my class 1 by 1, and in the `DateTime` field, I change it to `DateTime.ToString()`. I hope I could find a better solution – Mark Jul 18 '18 at 10:21
  • try this var date = DateTime.Now; date.ToString("dd/mm/yyyy h:mm:ss")); – Hitesh Anshani Jul 18 '18 at 10:26
  • 1
    @D-johnAnshani please try to understand the context of the question. It's about ASP.NET MVC serializing a response object. There's no manual string manipulation there. – CodeCaster Jul 18 '18 at 10:27
  • oh yes @CodeCaster – Hitesh Anshani Jul 18 '18 at 10:29
  • Which json converter are you using? The Newtonsoft one should do this out of the box. – Neil Jul 18 '18 at 10:31
  • @Neil yes I am using newtonsoft. Still with no luck – Mark Jul 18 '18 at 10:38
  • 2
    Did you define JSON.Net to be used as serializer, like shown here https://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc ? The default for json serialization is still .NET's own JavaScriptSerializer. – derpirscher Jul 18 '18 at 10:49
  • @derpischer I cant log in to my computer now. i would try it as soon as possible. – Mark Jul 18 '18 at 11:25

1 Answers1

0

Try to convert object to JSON using this settings:

var settings = new JsonSerializerSettings
{
    DateFormatHandling = DateFormatHandling.IsoDateFormat
};

string json = JsonConvert.SerializeObject(data, settings);

https://www.newtonsoft.com/json/help/html/SerializeDateFormatHandling.htm