Reputation: 15036
I have this classes:
public class EncuestaRespuesta
{
public string idCaso { get; set; }
public string tipoEncuesta { get; set; }
public string codMovimiento { get; set; }
public string motivoRetirada { get; set; }
public string duracion { get; set; }
public DateTime fechaHora { get; set; }
public string gestor { get; set; }
public List<RespuestasEncuesta> listRespuestasEncuestas { get; set;}
}
public class RespuestasEncuesta
{
public string pregunta { get; set; }
public string respuesta { get; set; }
public string numOrdenTrabajo { get; set; }
}
Using JSON.NET,
// https://www.newtonsoft.com/json/help/html/NullValueHandlingIgnore.htm
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
settings.Converters.Add(new Newtonsoft.Json.Converters.IsoDateTimeConverter());
var json = JsonConvert.SerializeObject(request, Formatting.Indented, settings);
When I call REST API (HttpClient PostAsync
), I get the error:
"The server committed a protocol violation. Section=ResponseStatusLine" Error: ServerProtocolViolation
I don't why. My JSON string:
{
"idCaso": "5009E000007Hp94QAD",
"codMovimiento": "1",
"motivoRetirada": "2",
"duracion": "0",
"fechaHora": "2018-03-20T12:56:36.4841861Z",
"gestor": "GESTOR SALESFORCE",
"tipoEncuesta": "T3_1018_RM",
"listRespuestasEncuestas": [
{
"numOrdenTrabajo": "",
"pregunta": "",
"respuesta": ""
}
]
}
I would like this JSON string, is OK in my tests:
{
"idCaso": "5009E000007Hp94QAC",
"codMovimiento": "1",
"motivoRetirada": "1",
"duracion": "",
"fechaHora": "2018-02-06T14:40:43.511Z",
"gestor": "",
"tipoEncuesta": "T3_1018_RM",
"listRespuestasEncuestas": [
{
"numOrdenTrabajo": "",
"pregunta": "",
"respuesta": ""
}
]
}
Issue: The dateTime field:
"fechaHora": "2018-03-20T12:56:36.4841861Z",
is wrong
"fechaHora": "2018-02-06T14:40:43.511Z"
is OK
Any suggestions?
Upvotes: 0
Views: 7035
Reputation: 15036
I use IsoDateTimeConverter
with DateTimeFormat
:
// https://www.newtonsoft.com/json/help/html/NullValueHandlingIgnore.htm
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
settings.Converters.Add(new Newtonsoft.Json.Converters.IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.FFFZ" });
var json = JsonConvert.SerializeObject(request, Formatting.Indented, settings);
Dates JSON
https://www.newtonsoft.com/json/help/html/DatesInJSON.htm
how to force netwtonsoft json serializer to serialize datetime property to string?
Serializing multiple DateTime properties in the same class using different formats for each one
Newtonsoft.json IsoDateTimeConverter and DateFormatHandling
DateTime Format UTC
How can I format DateTime to web UTC format?
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
Upvotes: 3