使用Json.Net转换DataTable时,使用Json的格式无效 [英] Invalid format with Json when converting DataTable with Json.Net
问题描述
我正在尝试使用Newtonsoft.JSON将DataTable转换为JSON,但发现输出不是ExtJS网格和图表所期望的.
I'm trying to convert DataTable to JSON using Newtonsoft.JSON but found that the output is not what ExtJS grid and chart would expect.
我的代码是
string output = JsonConvert.SerializeObject(dt, Formatting.Indented,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
这将返回Json字符串为
and this returns Json string as
"[{\"DAYDATE\":\"2012-05-22T00:15:00\",\"SERIES1\":3.65}]"
如果我删除'\'并在双引号和结束双引号中使用ExtJS,则效果很好.
If I remove '\' and start and end double quotes it works fine with ExtJS.
我还尝试将日期格式更改为更多JSON'y
I also tried changing date format to more JSON'y
string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
产生
"[{\"DAYDATE\":new Date(1337642100000),\"SERIES1\":3.65}]"
仍然没有运气
推荐答案
您的JSON似乎已经被双重序列化了.尽管您没有显示完整的控制器代码,但我猜测您正在执行以下操作:
It looks like your JSON is getting double serialized. Although you did not show your full controller code, I'm guessing that you are doing something like this:
public ActionResult GetDataTable()
{
// (... code to build data table omitted for brevity ...)
// Serialize data table using Json.Net to avoid circular reference error
string output = JsonConvert.SerializeObject(dt,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented
});
return Json(output);
}
Json()
方法还调用序列化.通常,在MVC控制器中,您将只使用Json()
方法来序列化您的返回对象,而不是单独使用Json.Net.我可以看到您在这里使用Json.Net来尝试解决序列化数据表时由于循环引用而发生的异常.如果要手动进行序列化,则需要以不会再次进行序列化的方式返回数据.您可以改为使用Content()
方法来执行此操作.像这样尝试:
The Json()
method also invokes serialization. Normally, in an MVC controller, you would just use the Json()
method to serialize your return object, and not use Json.Net separately. I can see you are using Json.Net here to try to get around the exception that happens due to circular references when you try to serialize a data table. If you are going to serialize manually, then you need to return the data in a way that it will not get serialized a second time. You can do this using the Content()
method instead. Try it like this:
public ActionResult GetDataTable()
{
// Build data table
DataTable dt = new DataTable();
dt.Columns.Add("DAYDATE", typeof(DateTime));
dt.Columns.Add("SERIES1", typeof(double));
dt.Rows.Add(new DateTime(2012, 5, 22, 0, 15, 0), 3.65);
// Serialize data table using Json.Net to avoid circular reference error
string output = JsonConvert.SerializeObject(dt,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Formatting.Indented
});
// Output is already serialized; return it as is (with the appropriate media type)
return Content(output, "application/json");
}
在我的测试中,以上内容将产生以下输出,我认为这是您想要的:
In my testing, the above will produce the following output, which I think is what you are looking for:
[ { "DAYDATE": "2012-05-22T00:15:00", "SERIES1": 3.65 } ]
这篇关于使用Json.Net转换DataTable时,使用Json的格式无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!