【问题标题】:Getting the dataset in Json instead of Xml ASP.NET Web service在 Json 而不是 Xml ASP.NET Web 服务中获取数据集
【发布时间】:2016-01-15 17:06:08
【问题描述】:

我还是 asp.net 的新手,我目前正在尝试创建一个 Web 服务,该服务将从我的 SQL 数据库中获取数据,以便在 android 应用程序中显示它。

我现在能够从 SQL 过程(获取客户信息)中获取我想要的数据,并且响应是 XML 格式的。

我搜索了很多关于一个很好的教程,该教程展示了如何在没有运气的情况下以 JSON 而不是 XML 获取响应(我还尝试了一些关于堆栈溢出的答案),但关于数据集的内容却很少。

这是我到目前为止的代码,任何帮助将不胜感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace ART
{    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService1 : System.Web.Services.WebService
    {
        SqlCommand cmdselect = null, cmdinserted;
        SqlConnection con4, converiler, con3, con;
        SqlDataAdapter da4, da, da3;
        SqlDataReader dr;    

        [WebMethod]         
        public DataSet AboneKullanici(int pkAbone)
        {    
            SqlConnection conFriends = new SqlConnection("Data Source=.;Initial Catalog=TTL;Integrated Security=True;");
            DataSet dsvrs = new DataSet();
            cmdselect = new SqlCommand();
            cmdselect.CommandText = "spAboneKullanici";
            cmdselect.CommandTimeout = 0;
            cmdselect.CommandType = CommandType.StoredProcedure;
            cmdselect.Connection = conFriends;
            da = new SqlDataAdapter(cmdselect);
            cmdselect.Parameters.Add("@aboneid", SqlDbType.Int, 10).Value = pkAbone;
            conFriends.Open();
            da.Fill(dsvrs, "kullaniclar");
            conFriends.Close();
            return dsvrs;
        }
    }
}

【问题讨论】:

    标签: asp.net json web-services datatable webmethod


    【解决方案1】:

    我会这样做(VB.Net):

    'dt is the data table with your data
    Dim js As New JavaScriptSerializer
    Return js.Serialize(dt.Rows)
    

    【讨论】:

      【解决方案2】:

      你可以这样做

      • 设置函数描述符以返回 JSON
      • 将您的数据查询到DataTable
      • 将您的DataTable 转换为Dictionary
      • 将Dictionary 序列化为JSON

      就是这样

      [WebMethod()]
      [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
      public static string AboneKullanici(int pkAbone) {
          SqlConnection conFriends = new SqlConnection("Data Source=.;Initial Catalog=TTL;Integrated Security=True;");
          DataTable dsvrs = new DataTable("kullaniclar");
          cmdselect = new SqlCommand();
          cmdselect.CommandText = "spAboneKullanici";
          cmdselect.CommandTimeout = 0;
          cmdselect.CommandType = CommandType.StoredProcedure;
          cmdselect.Connection = conFriends;
          da = new SqlDataAdapter(cmdselect);
          cmdselect.Parameters.Add("@aboneid", SqlDbType.Int, 10).Value = pkAbone;
          conFriends.Open();
          da.Fill(dsvrs);
          conFriends.Close();
      
          System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
          List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
          Dictionary<string, object> row;
          foreach (DataRow dr in dsvrs.Rows) {
              row = new Dictionary<string, object>();
              foreach (DataColumn col in dsvrs.Columns) {
                  row.Add(col.ColumnName, dr[col]);
              }
              rows.Add(row);
          }
          return serializer.Serialize(rows);
      }
      

      【讨论】:

      • 既然你是asp.net的新手,我建议你看看web api。您使用的 Web 服务技术已经过时。如果您只需要 json,那么只需使用 web api。
      • @Don 你能不能给我一些链接,我可以看看你的建议(我知道它已经过时了,但我的教授坚持使用这种方式,因为这是他得到的方式用于)
      • @96ASP43 这是一个小错字检查更新代码foreach (DataColumn col in dsvrs.Columns) {
      • 只需访问 asp.net 网站并查看 web api 或查看此链接,asp.net/web-api/overview/getting-started-with-aspnet-web-api/…
      • @haraman 非常感谢您的帮助,我发誓,您让我度过了美好的一天,
      猜你喜欢
      • 2012-02-26
      • 2023-04-05
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      相关资源
      最近更新 更多