0
我使用C#.NET .NET Web服務,並將此Web服務的結果發佈到JSON數組變量中,但是我的結果中有一些奇怪的地方,那就是它不是純JSON變量。如何將其更改爲純JSON變量?如何將Web服務的結果更改爲純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();
}
這裏是我當前的Web服務結果:
-<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"}]}
所以你如何去除該XML格式?你建立另一個網絡服務?你說過你想在android上使用它,我也是。你如何解決這個問題? – blankon91
是的,我建立了另一個使用WCF的webservice。使用ASMX網絡服務,你不能得到純JSON,因此我切換到WCF ..這也是類似的,也容易在Android上使用..我會找到很多鏈接/代碼爲相同的 –
好吧,我會嘗試使用WCF創建另一個Web服務。謝謝 :) – blankon91