从 SqlDataReader 转换为 JSON
public string toJSON(SqlDataReader o)
{
StringBuilder s = new StringBuilder();
s.Append("[");
if (o.HasRows)
while (o.Read())
s.Append("{" + '"' + "Id" + '"' + ":" + o["Id"] + ", "
+ '"' + "CN" + '"' + ":" + o["CatName"] + ", "
+ '"' + "Ord" + '"' + ":" + o["Ord"] + ","
+ '"' + "Icon" + '"' + ":" + o["Icon"] + "}, ");
s.Remove(s.Length - 2, 2);
s.Append("]");
o.Close();
return s.ToString();
}
我在这里使用我自己的函数来进行序列化。我需要知道这是否是一个好方法,或者我应该使用另一种方法。顺便说一句,我尝试使用 JavaScriptSerializer
但这不适用于 SqlDataReader 。谢谢
public string toJSON(SqlDataReader o)
{
StringBuilder s = new StringBuilder();
s.Append("[");
if (o.HasRows)
while (o.Read())
s.Append("{" + '"' + "Id" + '"' + ":" + o["Id"] + ", "
+ '"' + "CN" + '"' + ":" + o["CatName"] + ", "
+ '"' + "Ord" + '"' + ":" + o["Ord"] + ","
+ '"' + "Icon" + '"' + ":" + o["Icon"] + "}, ");
s.Remove(s.Length - 2, 2);
s.Append("]");
o.Close();
return s.ToString();
}
I'm using here my own function to do serialization . I need to know if this is a good way or I should use another . BTW I've tried to use the JavaScriptSerializer
but this didn't work with SqlDataReader . thanx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您想要转换为任意 JSON 的内容,您可以通过将其序列化为字典(字符串,对象)来进行转换:
然后使用 NewtonSoft.Json JsonConvert 对象获取您的 JSON:
更新:
如果您只想使用内置方法,并且恰好使用 MVC,则可以在新序列化的 上使用内置 Json 帮助器方法:
If you want something that'll convert to arbitrary JSON, you could convert by serializing it into a Dictionary(Of string, object) thusly:
And then use the NewtonSoft.Json JsonConvert object to get your JSON:
UPDATE:
If you just want to use built-in methods, and you happen to be using MVC, you can use the built in Json helper method on your newly serialized :
这应该可以完成工作
This should do the job
我遇到过一些用例,其中数据读取器返回的行数可能会在内存消耗方面出现问题。以下代码在流上使用 JsonWriter(来自 JSON.NET)。人们当然可以争论大量 JSON 文档的实用性,但有时我们的用例是由其他人决定的:-)
一些注意事项:
代码:
异构输出的示例如下:
参考:
I encounter use cases where the number of rows being returned by the data reader may become problematic with respect to memory consumption. The following code uses a JsonWriter (from JSON.NET) over a stream. One can certainly debate the utility of enormous JSON documents, but sometimes our use cases are dictated by others :-)
A few notes:
The code:
An example of heterogeneous output would be:
Reference:
另一种选择是使用 James Newton-King 出色的 JSON.NET 库 - http://www.newtonsoft.com/json
下面是一个关于如何使用它构建集合然后将其输出为 JSON 序列化字符串的简单示例:
您可以通过将 SqlDataReader 的每一行读入匿名对象来对循环执行相同的操作然后使用 JSON.NET 将其序列化为字符串。
希望这有帮助!
Another option would be to use James Newton-King's excellent JSON.NET library - http://www.newtonsoft.com/json
Here's a quick example on how to use it to build up a collection and then output it as a JSON-serialized string:
You could do the same with your looping by reading in each row of your SqlDataReader into an anonymous object and then use JSON.NET to serialize it to a string.
Hope this helps!
从 SQL Server 2016 开始,微软在 SQL 查询中嵌入了此功能。您可以通过在查询末尾使用
FOR JSON
关键字来实现。有关更多详细信息和示例,您可以浏览此官方文档 使用自动模式自动格式化 JSON 输出 (SQL Server)
这里是来自 Microsoft 的 C# 代码示例,用于从 SQL 查询中获取 JSON 字符串。
警告:此解决方案仅对 SQL SERVER 2016 及更高版本有效。
Since SQL Server 2016, Microsoft embedded this feature with sql queries. You can achieve it by using
FOR JSON
keyword at the end of your queries.for more details and example you can go through this official documents Format JSON Output Automatically with AUTO Mode (SQL Server)
Here is the C# code example from Microsoft to get JSON string from SQL queries.
Warning: This solution is only valid for SQL SERVER 2016 and higher.
我根据乔纳森的回答使用此代码:
然后:
I use this code, based on Jonathan's answer:
And then:
试试这个:
Try this:
使用 Cinchoo ETL - 一个开源库,您可以轻松地将 SqlDataReader 导出为 JSON代码行
输出:
With Cinchoo ETL - an open source library, you can export SqlDataReader to JSON easily with few lines of code
Output:
这是为了增强 Chandu 的使用查询语法的 Linq 答案(from ... select ...)。如果您更喜欢方法语法,这里就是您的答案。
希望它对某人有帮助。
This is to enhance Chandu's Linq answer that uses query syntax (from ... select ...). If you prefer Method Syntax, here is your answer.
Hope it helps somebody.
这没那么难。当我想将搜索结果作为 JSON 返回到网页时,我就是这么做的。
首先,有一个这样的类
,然后有下面的代码。
这对我来说非常有效..
This can't be that hard. This is what I've done when I want to return search results to a web page as JSON.
First, have a class like this
and then have the code below.
this works for me very well..
根据 Jonathan's 的回答,我在 ASP.NET Core 中有类似的要求,将 SQLDataReader 的结果转换为 JSON 字符串或结果对象,
所以我为它创建了一个扩展方法:
&按照我的要求使用它:
或者
我创建了一个异步方法,因为在从数据库读取完成之前我还有其他一些事情要做。
Further to Jonathan's Answer, I had a similar requirement in ASP.NET Core to convert the result of an SQLDataReader to a JSON string or a Result Object,
so I created an extension method for it as :
& used it as per my requirement as :
or
I created an async method because I had some other things to be done till the reading was finished from database.
我创建了以下方法,它将任何 DataReader 转换为 JSON,但仅适用于单深度序列化:
您应该将读取器和列名称作为字符串数组传递,例如:
然后调用该方法
I made the following method where it converts any DataReader to JSON, but only for single depth serialization:
you should pass the reader, and the column names as a string array, for example:
then call the method
添加引用:
System.Web.Extensions
到项目然后
在 C# 代码中,您可以使用 write :
add reference :
System.Web.Extensions
to projectthen
in c# code, you can use write :