在 Json 而不是 Xml ASP.NET Web 服务中获取数据集

Getting the dataset in Json instead of Xml ASP.NET Web service

我还是 asp.net 的新手,目前我正在尝试创建一个 Web 服务,它将从我的 SQL 数据库中获取数据,以便在 [=23] 中显示=] 申请。

我现在可以从 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;
        }
    }
}

你可以这样做

  • 将函数描述符设置为 return 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);
}

我会这样做 (VB.Net):

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