I have a model "data" which contains some booleans and strings. I want this model returned with a HttpResponseMessage. Currently I am doing it like this:
string JSON = JsonConvert.SerializeObject(Data);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, JSON);
return response;
This results in the following JSON output "{\"\"}"
. But I need it to be {""}
.
Yet if I only show the JSON before the HttpResponseMessage
it is like {""}
and I don't seem to find out what causes the output format to change. Does anyone know what causes this and how to solve this?
It seems to me the JSON gets 'stringitized' but why I don't know.
I am using NewtonSoft.
This happens because the Web API framework has built-in serialization, and you are manually serializing your data on top of that. This double-serialization results in the extra backslashes and quotes you see in the response JSON.
To fix your code, remove the call to
SerializeObject
and pass yourData
object directly toRequest.CreateResponse
like this: