【问题标题】:How to return a JSON-encoded array from a .NET web service?如何从 .NET Web 服务返回 JSON 编码的数组?
【发布时间】:2012-06-21 07:21:47
【问题描述】:

我已经使用 JSON 创建了一个 .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(); 
    }

【问题讨论】:

    标签: .net arrays json web-services


    【解决方案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 语句中。

    【讨论】:

    • 现在我明白了,非常感谢您的解释:) 但是,在您的第二个示例中,结果没有显示为 JSON 变量,我想要 JSON 变量中的结果,所以我使用你的第一个例子。再次感谢@Darin
    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 2012-07-11
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2023-03-25
    相关资源
    最近更新 更多