如何从.NET Web服务返回JSON编码的数组?

时间:2022-06-01 19:12:24

I've create a .NET web service with JSON. but the result didn't show as array. how to make the JSON result into array in my web service?

我用JSON创建了一个.NET Web服务。但结果没有显示为数组。如何在我的Web服务中将JSON结果转换为数组?

here is my web service code:

这是我的网络服务代码:

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

1 个解决方案

#1


1  

You are serializing a single instance of the Report object, not an array. So the first step is to build an array:

您正在序列化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(); 
}

and the second step of course is to do this properly and get rid of any plumbing code in your method and leave this to the infrastructure:

当然第二步是正确地执行此操作并删除方法中的任何管道代码并将其留给基础结构:

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

You will also notice in my second example that the proper way to dispose IDisposable resources is to always wrap them in using statements.

您还将在我的第二个示例中注意到,处理IDisposable资源的正确方法是始终将它们包装在using语句中。

#1


1  

You are serializing a single instance of the Report object, not an array. So the first step is to build an array:

您正在序列化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(); 
}

and the second step of course is to do this properly and get rid of any plumbing code in your method and leave this to the infrastructure:

当然第二步是正确地执行此操作并删除方法中的任何管道代码并将其留给基础结构:

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

You will also notice in my second example that the proper way to dispose IDisposable resources is to always wrap them in using statements.

您还将在我的第二个示例中注意到,处理IDisposable资源的正确方法是始终将它们包装在using语句中。