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();
}
它的工作原理,謝謝@Darin :) – blankon91
現在我明白了,非常感謝您的解釋:) 但是,在第二個示例中,結果沒有顯示爲JSON變量,我想要結果JSON變量,所以我使用你的第一個例子。再次感謝@Darin – blankon91