Reputation: 317
I created a service to do database lookup and return result as JSON, using Nancy. Here is test code:
using (var dt = new DataTable("MyData"))
{
dt.Columns.Add("id");
dt.Columns.Add("password");
dt.Columns.Add("body", System.Type.GetType("System.String"));
dt.Columns.Add("balance", System.Type.GetType("System.Double"));
dt.Rows.Add(uid, pwd, "Some useful content", 33.75);
dt.Rows.Add(uid, pwd, "More useful content", 128.55);
if (dotNetDataTable)
json = JsonConvert.SerializeObject(dt, new Serialization.DataTableConverter());
else
json = JsonConvert.SerializeObject(dt);
json = "{\"status\":\"success\",\"data\":" + json + "}";
return Response.AsJson(json);
}
Interrupting process just before return, and polling value of json, I get:
"{\"status\":\"success\",\"data\":[{\"id\":\"RMittelman\",\"password\":\"Password\",\"body\":\"Some useful content\",\"balance\":33.75},{\"id\":\"RMittelman\",\"password\":\"Password\",\"body\":\"More useful content\",\"balance\":128.55}]}"
This seems correct in immediate window. When I receive data, and interrupt the process, I get this in the client immediate window:
"\"{\\\"status\\\":\\\"success\\\",\\\"data\\\":[{\\\"id\\\":\\\"RMittelman\\\",\\\"password\\\":\\\"Password\\\",\\\"body\\\":\\\"Some useful content\\\",\\\"balance\\\":33.75},{\\\"id\\\":\\\"RMittelman\\\",\\\"password\\\":\\\"Password\\\",\\\"body\\\":\\\"More useful content\\\",\\\"balance\\\":128.55}]}\""
Which as you can see contains extra leading and trailing quotes, and a bunch of extra backslashes. The JSON will not deserialize. Here is the result shown in client's text box:
json result:
"{\"status\":\"success\",\"data\":
[{\"id\":\"RMittelman\",\"password\":\"Password\",\"body\":\"Some useful content\",\"balance\":33.75},
{\"id\":\"RMittelman\",\"password\":\"Password\",\"body\":\"More useful content\",\"balance\":128.55}]}"
Unexpected JSON token when reading DataTable. Expected StartArray, got String. Path '', line 1, position 240.
If I programmatically remove leading and trailing quotes, and all backslashes, it works fine. Here is the code I use in client to get the data:
string GET(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
string responseText = reader.ReadToEnd();
isError = false;
return responseText;
}
}
catch (WebException ex)
{
isError = true;
WebResponse errorResponse = ex.Response;
if (errorResponse == null)
return ex.Message;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
return errorText;
}
throw;
}
}
So question is, why does it look ok before sending, but contain all the junk after sending?
Upvotes: 0
Views: 2670
Reputation: 317
JSON was getting double-encoded. Code was:
json=JsonConvert.SerializeObject(dataTable);
then
return Response.AsJson(json);
Changed it to:
return (Response)json;
and the problem went away. Thanks everybody for your input!
Upvotes: 3