2012-06-21 48 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