What should I use to serialize a DataTable to JSON in ASP.NET 2.0?

↘锁芯ラ 提交于 2019-12-07 05:06:16

问题


I'm trying to support a legacy app I made a year ago and need to use jQuery AJAX calls to get data from the server. I have a JsonHelper class that I use to serialize DataTables and DataSets. This normally works fine in .NET 4.0, but .NET 2.0 there is no System.Web.Script.Serialization library and therefore no JavaScriptSerializer class.

I've tried to find another JSON serializer, but so far I can't get anything to work.

I tried using JSON.NET 2.0 http://json.codeplex.com/releases/view/13099 but it said it needed a higher framework. So I tried JSON.NET 1.3.1 http://json.codeplex.com/releases/view/6987 I could only figure out how to write JSON to a .txt file, and I'd like to just be able to pass the string back to the client.

I also just tried JsonFx.NET http://jsonfx.net/download/ serialization library (for 2.0). Still couldn't get anything to work.

I don't know if I'm missing some crucial element of these libraries or if there is an easy solution.

Thanks in advance


回答1:


I used this question's first answer to solve my problem: DataTable to JSON

I downloaded the AJAX Extensions 1.0 Library and included "System.Web.Extensions.dll" with my project. http://www.microsoft.com/download/en/details.aspx?id=883

JavaScriptSerializer now works.




回答2:


The Jayrock Library works with all versions of .Net all the way down to 1.x. You'll have to build it yourself for 2.0, but that's no big deal.

I've used it for the past few months. Nice and simple Json serialization and deserialization.




回答3:


Easy (but not elegant) solution is to create a shim in .NET 4 that will call your legacy application via HTTP POST or SOAP and convert result in JSON format with NewtonSoft.Json which works OK on .NET 4.



来源:https://stackoverflow.com/questions/8592294/what-should-i-use-to-serialize-a-datatable-to-json-in-asp-net-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!