blankon91
blankon91

Reputation: 515

How to change the result from web service into pure JSON variable?

I've .NET web service using C#, and I post the result from this web service in JSON Array variable, but there is something strange with my result its not pure JSON variable. How to change it into pure JSON variable?

here is my code:

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

here is my current web service result:

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

and I want to change it into:

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

Upvotes: 0

Views: 675

Answers (1)

Parth Doshi
Parth Doshi

Reputation: 4208

Try building a WCF service. Refer Making of JSON Webservice using C#.NET.

You can learn to create WCF services using this.

Hope it helps.

Upvotes: 1

Related Questions