0

我有使用 C# 的 .NET Web 服务,并将此 Web 服务的结果发布在 JSON 数组变量中,但我的结果有些奇怪,它不是纯 JSON 变量。如何将其更改为纯 JSON 变量?

这是我的代码:

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public ModelReport.Report[] GetReports()
    {
        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);

            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();
    }

这是我当前的网络服务结果:

-<string>[{"total":"209480","type":"ESL500ML"},{"total":"10177","type":"CHEESE1K"},{"total":"2719928","type":"ESL"},{"total":"145920","type":"WHP"},{"total":"417236.136","type":"UHT"}]</string>

我想把它改成:

{"report":[{"total":"209480","type":"ESL500ML"},{"total":"10177","type":"CHEESE1K"},{"total":"2719928","type":"ESL"},{"total":"145920","type":"WHP"},{"total":"417236.136","type":"UHT"}]}
4

1 回答 1

1

尝试构建一个WCF service. 请参阅使用 C#.NET 制作 JSON Web 服务

您可以学习使用创建 WCF 服务。

希望能帮助到你。

于 2012-06-25T03:48:55.227 回答