2012-06-21 66 views
1

我用JSON創建了一個.NET Web服務。但結果並未顯示爲數組。如何將JSON結果放入我的Web服務中的數組中?如何從.NET Web服務返回JSON編碼的數組?

這裏是我的web服務代碼:

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public String GetReport() 
    { 
     ModelReport.Report report = new ModelReport.Report(); 
     string connectionString = ConfigurationManager.ConnectionStrings["ConnWf"].ConnectionString; 
     using (SqlConnection connection = new SqlConnection(connectionString)) 
     { 
      string sql = "select type, sum(OrderQty) as total from tbl_weeklyflash_ID where type <> 'NULL' group by type"; 
      connection.Open(); 

      SqlCommand command = new SqlCommand(sql, connection); 
      SqlDataReader reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       report.type = reader["type"].ToString(); 
       report.total = reader["total"].ToString(); 
      } 
     } 

     MemoryStream stream = new MemoryStream(); 
     DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ModelReport.Report)); 
     serializer.WriteObject(stream, report); 
     stream.Position = 0; 
     StreamReader streamReader = new StreamReader(stream); 
     return streamReader.ReadToEnd(); 
    } 

回答

1

您序列化Report對象,而不是一個數組的單個實例。所以第一步是建立一個數組:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public String GetReport() 
{ 
    List<ModelReport.Report> reports = new List<ModelReport.Report>(); 
    string connectionString = ConfigurationManager.ConnectionStrings["ConnWf"].ConnectionString; 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    { 
     string sql = "select type, sum(OrderQty) as total from tbl_weeklyflash_ID where type <> 'NULL' group by type"; 
     connection.Open(); 

     SqlCommand command = new SqlCommand(sql, connection); 
     SqlDataReader reader = command.ExecuteReader(); 
     while (reader.Read()) 
     { 
      ModelReport.Report report = new ModelReport.Report(); 
      report.type = reader["type"].ToString(); 
      report.total = reader["total"].ToString(); 
      reports.Add(report); 
     } 
    } 

    MemoryStream stream = new MemoryStream(); 
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ModelReport.Report[])); 
    serializer.WriteObject(stream, reports.ToArray()); 
    stream.Position = 0; 
    StreamReader streamReader = new StreamReader(stream); 
    return streamReader.ReadToEnd(); 
} 

,當然還有第二個步驟是正確地做到這一點,並擺脫在你的方法任何管道代碼,並離開這個基礎設施:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public ModelReport.Report[] GetReport() 
{ 
    List<ModelReport.Report> reports = new List<ModelReport.Report>(); 
    string connectionString = ConfigurationManager.ConnectionStrings["ConnWf"].ConnectionString; 
    using (SqlConnection connection = new SqlConnection(connectionString)) 
    using (SqlCommand command = connection.CreateCommand) 
    { 
     string sql = "select type, sum(OrderQty) as total from tbl_weeklyflash_ID where type <> 'NULL' group by type"; 
     connection.Open(); 

     command.CommandText = sql; 
     using (SqlDataReader reader = command.ExecuteReader()) 
     { 
      while (reader.Read()) 
      { 
       ModelReport.Report report = new ModelReport.Report(); 
       report.type = reader["type"].ToString(); 
       report.total = reader["total"].ToString(); 
       reports.Add(report); 
      } 
     } 
    } 

    return reports.ToArray(); 
} 

在我的第二個示例中,您還會注意到處置IDisposable資源的正確方法是始終將它們包裝在using語句中。

+0

它的工作原理,謝謝@Darin :) – blankon91

+0

現在我明白了,非常感謝您的解釋:) 但是,在第二個示例中,結果沒有顯示爲JSON變量,我想要結果JSON變量,所以我使用你的第一個例子。再次感謝@Darin – blankon91

相關問題