2011-11-12 109 views
0

我正在jquery上獲取數據綁定到dropdownist &也我使用了web服務方法,它的工作很好,但我沒有得到一些解釋,我將綁定數據一個dropdownlist我從Web方法獲取數據返回到Array對象和另一個下拉列表我從Web方法獲取數據,這是從JSON對象返回的,但在前端我沒有任何區別。他們中的大多數都告訴序列化json的好方法,所以這裏究竟發生了什麼?我有點迷惑 請幫我 謝謝瞭解ToArray和JSON之間的區別

**here is my code** 
Default.aspx 

<html> 
<head runat="server"> 
    <title>JsonAndToArray</title> 

    <script type="text/javascript" src="js/jquery.min.js"></script> 

    <script type="text/javascript"> 

     $(document).ready(function() { 
      ddlActivityType = document.getElementById("<%=ddlActivity.ClientID %>"); 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json;charset/utf-8", 
       url: "Visit.asmx/GetActivityByJSON", 
       dataType: "json", 
       success: function(results) { 
        results = eval(results.d); 
        $('#ddlActivity').get(0).options.length = 0; 
        $('#ddlActivity').get(0).options[0] = new Option(' --select-- ', '0'); 
        $.each(results, function(val, text) { 
         $('#ddlActivity').append($('<option></option>').val(text[1]).html(text[0])); 
        }); 
       } 
      }); 

      ddlActivityType1 = document.getElementById("<%=ddlActivity2.ClientID %>"); 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json;charset/utf-8", 
       url: "Visit.asmx/GetActivity", 
       dataType: "json", 
       success: function(results) { 
        results = eval(results.d); 
        $('#ddlActivity2').get(0).options.length = 0; 
        $('#ddlActivity2').get(0).options[0] = new Option('--select--', '0'); 
        $.each(results, function(val, text) { 
         $('#ddlActivity2').append($('<option></option>').val(text[1]).html(text[0])); 
        }); 
       } 
      }); 
     }); 

    </script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    Json-Activity : 
    <select id="ddlActivity" runat="server"> 
    </select> 
    <br /> 
    <br /> 
    ToArray-Activity : 
    <select id="ddlActivity2" runat="server"> 
    </select> 
    <br /> 
    <br /> 
    <asp:Button ID="btnJson" runat="server" Text="Json" OnClick="Json_click"/> 
    </form> 
</body> 
</html> 


Defalut.aspx.cs 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.Script.Serialization; 

public partial class Defalut: System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void Json_click(object sender,EventArgs e) 
    { 
    } 
} 


**webservices** 

** 

- Visit.asmx 

** 


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.Serialization; 
using Facade; 

/// <summary> 
/// Summary description for Visit 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService] 
public class Visit : System.Web.Services.WebService 
{ 

    public Visit() 
    { 

     //Uncomment the following line if using designed components 
     //InitializeComponent(); 
    } 

    [WebMethod] 
    public string HelloWorld() 
    { 
     return "Hello World"; 
    } 

    [WebMethod] 
    public IList<string[]> GetActivity() 
    { 
     IList<string[]> values = new List<string[]>(); 
     //string value = ""; 
     try 
     { 
      SqlConnection con_New = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog="Database";Integrated Security=True;"); 
      con_New.Open(); 
      SqlCommand cmdSelect_ST = new SqlCommand("select id,name from table", con_New); 
      SqlDataAdapter da_ST = new SqlDataAdapter(cmdSelect_ST); 

      DataSet ds = new DataSet(); 
      da_ST.Fill(ds); 
      DataTable dt = ds.Tables[0]; 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       string[] ActivityType = new string[2]; 
       ActivityType[0] = dt.Rows[i]["name"].ToString(); 
       ActivityType[1] = dt.Rows[i]["id"].ToString(); 
       values.Add(ActivityType); 
      }   
     } 
     catch (Exception ex) 
     { 

     } 
     return values; 
    } 


    [WebMethod] 
    public string GetActivityByJSON() 
    { 
     IList<string[]> values = new List<string[]>(); 
     string value = ""; 
     try 
     { 
      SqlConnection con_New = new SqlConnection(@"Data Source=SQLEXPRESS;Initial Catalog="Database";Integrated Security=True;"); 
      con_New.Open(); 
      SqlCommand cmdSelect_ST = new SqlCommand("select name,id from table", con_New); 
      SqlDataAdapter da_ST = new SqlDataAdapter(cmdSelect_ST); 

      DataSet ds = new DataSet(); 
      da_ST.Fill(ds); 
      DataTable dt = ds.Tables[0]; 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       string[] ActivityType = new string[2]; 
       ActivityType[0] = dt.Rows[i]["name"].ToString(); 
       ActivityType[1] = dt.Rows[i]["id"].ToString(); 
       values.Add(ActivityType); 
      } 
      JavaScriptSerializer js = new JavaScriptSerializer(); 
      value = js.Serialize(values); 
     } 
     catch (Exception ex) 
     { 

     } 
     return value; 
    } 
} 

回答

0

在第一次調用你正在返回其被系統轉換成JSON數組的數組。您將返回此:

IList<string[]> values = new List<string[]>(); 

在您轉換爲jsaon陣列並返回一個字符串的第二個電話。下面是執行轉換的代碼:

JavaScriptSerializer js = new JavaScriptSerializer(); 
value = js.Serialize(values); 

由於JSON只是以某種方式格式化字符串 - 沒有區別。就在創建發回的字符串時,以及誰(您的代碼或系統代碼)創建它。

+0

然後沒有必要做json序列化和發回到前端,我們可以使用To Array來代替它,它是否正確的方式我明白了? –

+0

呃...有點。系統會爲你做json序列化。 ToArray()不能代替任何東西。 – Hogan

+0

哦......就像那樣,謝謝 –