我们如何将 SOAP 网络服务的 return 类型指定为 JSON 格式?
How can we specify the return type of a SOAP webservice as JSON format?
这是我的 return xml 格式的方法,但我需要 return Json 类型格式。
[WebMethod]
public string GetAllCity(long UID, string IMEINo)
{
DataSet ds = new DataSet();
ExeWeb2 cd = new ExeWeb2();
DataTable dt = new DataTable();
//booking
string qyr = "";
// self booking
ds = cd.retData("select city_name from city order by city_name");
dt = ds.Tables[0];
DataSet ds1 = new DataSet();
ds1.Tables.Add(dt.Copy());
ds1.Tables[0].TableName = "City";
System.IO.MemoryStream s = new System.IO.MemoryStream();
ds1.WriteXml(s, XmlWriteMode.IgnoreSchema);
return System.Text.Encoding.UTF8.GetString(s.ToArray());
}
试试这个:
DataTable dt11 = ds1.Tables[0];
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt11.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt11.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
这是我的 return xml 格式的方法,但我需要 return Json 类型格式。
[WebMethod]
public string GetAllCity(long UID, string IMEINo)
{
DataSet ds = new DataSet();
ExeWeb2 cd = new ExeWeb2();
DataTable dt = new DataTable();
//booking
string qyr = "";
// self booking
ds = cd.retData("select city_name from city order by city_name");
dt = ds.Tables[0];
DataSet ds1 = new DataSet();
ds1.Tables.Add(dt.Copy());
ds1.Tables[0].TableName = "City";
System.IO.MemoryStream s = new System.IO.MemoryStream();
ds1.WriteXml(s, XmlWriteMode.IgnoreSchema);
return System.Text.Encoding.UTF8.GetString(s.ToArray());
}
试试这个:
DataTable dt11 = ds1.Tables[0];
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in dt11.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt11.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);