繁体   English   中英

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

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

我对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;
        }
    }
}

你可以这样

  • 设置函数描述符以返回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)

暂无
暂无

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

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