2015-10-16 93 views
0

我还是新的asp.net,和我目前正在创建一个Web服务,将让以显示它从我的SQL数据库中的数据在android应用程序中。获取数据集以JSON而不是XML的ASP.NET Web服务

我现在能够得到我从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; 
     } 
    } 
} 

回答

0

你能做到这样

  • 设置功能描述符JSON返回
  • 查询您的数据转换成DataTable
  • 将您DataTableDictionary
  • 序列化DictionaryJSON

就是这样

[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); 
} 
+0

既然你是新的asp.net,可能我建议你看看网页API。您使用的网络服务技术已过时。如果你需要的只是json,那么就使用web api。 – Don

+0

@Don你能不能请您给一些链接,我可以看看你有什么建议(我知道这是过时,但我的教授坚持用这种方式,因为这是他习惯的方式) – 96ASP43

+0

@ 96ASP43它是一个小的错字检查更新代码'foreach(DataColumn col in dsvrs.Columns){' – haraman

0

我会做到这一点(VB.Net):

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