如何将Web服务的结果更改为纯JSON变量?

时间:2012-06-25 03:33:08

标签: arrays json web-services

我使用C#的.NET Web服务,我将这个Web服务的结果发布在JSON Array变量中,但是我的结果有些奇怪,它不是纯粹的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"}]}

1 个答案:

答案 0 :(得分:1)

尝试构建WCF service。请参阅Making of JSON Webservice using C#.NET

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

希望它有所帮助。