【问题标题】:How to change the result from web service into pure JSON variable?如何将 Web 服务的结果更改为纯 JSON 变量?
【发布时间】:2012-06-25 03:33:07
【问题描述】:

我有一个使用 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"}]}

【问题讨论】:

    标签: arrays json web-services


    【解决方案1】:

    尝试建立一个WCF service。参考Making of JSON Webservice using C#.NET

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

    希望对你有帮助。

    【讨论】:

    • 那么您是如何删除该 xml 格式的呢?你建立另一个网络服务?你说你想在android上使用它,我也是。你是怎么解决这个问题的?
    • 是的,我使用 WCF 构建了另一个 Web 服务。使用 ASMX Web 服务,您无法获得纯 json,因此我切换到 WCF..这在 Android 上也很相似且易于使用..您会找到很多相同的链接/代码
    • 好的,我将尝试使用 WCF 创建另一个 Web 服务。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多