简体   繁体   English

如何从c#web服务返回数据表/数据集作为JSON

[英]How to return datatable / dataset from c# web service as JSON

I have web services returning strings as JSON by dint of using 我有Web服务通过使用返回字符串作为JSON

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

and calling them from the client with headers Content-type = application/json 并使用标题Content-type = application / json从客户端调用它们

this was all going well until I needed to return a table of results. 这一切都很顺利,直到我需要返回结果表。

I have tried using the code here http://www.west-wind.com/Weblog/posts/471835.aspx which succesfully encodes my dataset as JSON and returns a string, but this is in turn then encoded with escapes as part of the return from the .net service which is not what is wanted at all. 我已经尝试使用这里的代码http://www.west-wind.com/Weblog/posts/471835.aspx ,它成功地将我的数据集编码为JSON并返回一个字符串,但这又转换为带有转义符的一部分从.net服务返回,这根本不是想要的。 What type should my WebMethod return in order to get a datatable or dataset properly encoded as JSON ? 我的WebMethod应该返回什么类型才能将数据表或数据集正确编码为JSON?

Short answer, use eval on the string like: var ds = eval(responseText);. 简短回答,在字符串上使用eval,如:var ds = eval(responseText);.

I use Microsoft.Web.Preview.dll instead of Newtonsoft. 我使用Microsoft.Web.Preview.dll而不是Newtonsoft。 I feel Newtonsoft is a bit more code than Microsoft. 我觉得Newtonsoft的代码比微软要多一些。 And it sends you a string, not a JSON object. 它会向您发送一个字符串,而不是JSON对象。 Whereas in Microsoft you don't need to write all that extra code. 而在Microsoft中,您不需要编写所有额外的代码。 Download Microsoft.Web.Preview.dll (called ASP.NET Futures ). 下载Microsoft.Web.Preview.dll(称为ASP.NET Futures )。 Add the following to your web.config: 将以下内容添加到web.config:

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="33554432">
                <converters>
                    <add name="DataSetConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataSetConverter, Microsoft.Web.Preview"/>
                    <add name="DataRowConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataRowConverter, Microsoft.Web.Preview"/>
                    <add name="DataTableConverter" type="Microsoft.Web.Preview.Script.Serialization.Converters.DataTableConverter, Microsoft.Web.Preview"/>
                </converters>
            </jsonSerialization>
        </webServices>
    </scripting>
</system.web.extensions>

I have solved this by creating a custom class which represents the data I need to return, and then setting the return type of the service to a List . 我通过创建一个自定义类来解决这个问题,该类表示我需要返回的数据,然后将服务的返回类型设置为List。 This does involve an extra step to iterate through the datatable and build the list, but in the end I think I am happier returning the list of objects rather than the raw data - its probably a better practice anyway. 这确实需要一个额外的步骤来遍历数据表并构建列表,但最后我认为我更乐意返回对象列表而不是原始数据 - 无论如何它可能是更好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM