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