【发布时间】:2018-04-30 00:24:57
【问题描述】:
我正在使用 dot net framework 4.5 用 C# 编写的 restful API ..目前它工作正常...我在 JSON 转换后返回一个结果..我期待一个纯 JSON 结果...我目前没有得到。我期待一个简单的解决方案,在我返回 JSON 的根元素处省略字符串 XMLNS ...... 结果 我正进入(状态:
我的代码:
public String GetAllSalesInvoices(string customer_id, string Startdate, string Enddate)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string query = "SELECT * FROM sales_invoice WHERE customer_id =" + customer_id + " AND invoice_date BETWEEN '" + Startdate + "' AND '" + Enddate + "'";
DataSet ds = conObj.execQuery(query);
DataTable dt = new DataTable();
dt = ds.Tables[0];
List<sales_invoice> result = new List<sales_invoice>();
foreach (DataRow dr in dt.Rows)
{
sales_invoice inv = new sales_invoice()
{
Invoice_id = Convert.ToInt32(dr["invoice_id"]),
Invoice_date = Convert.ToString(dr["invoice_date"].ToString()),
Customer_id = Convert.ToInt32(dr["customer_id"]),
Product_id = Convert.ToInt32((dr["product_id"])),
Time = Convert.ToString((dr["time"]).ToString()),
Quantity = Convert.ToInt32((dr["quantity"])),
Unit_of_measure = Convert.ToString(dr["unit_of_measure"]),
Product_price = Convert.ToInt32((dr["product_price"])),
Sub_total = Convert.ToInt32((dr["sub_total"])),
};
result.Add(inv);
}
string json=serializer.Serialize(result);
return json;
}
谢谢
【问题讨论】:
-
我期待您编写的一些代码...请更新您的问题。
-
你在使用web api吗?如果是这样 - stackoverflow.com/questions/9847564/…
-
不,我正在享受宁静的网络服务@Steve
-
您的问题是您正在对
result进行双重序列化:首先您序列化为 JSON 字符串,然后 WCF 将字符串序列化为 XML。相反,让 WCF 为您序列化您的result,只需直接返回即可。然后,如果您想配置 WCF 以返回 JSON,请参阅 How to return Json from WCF Service? 或 WCF REST return single method as JSON and XML。 -
那么让它成为答案?