2015-10-16 96 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)