Sel*_*ara 7 c# xml rest wcf json
我正在使用dot net framework 4.5编写的用C#编写的restful API.当前它的工作正常......我在JSON转换后返回结果..我期待一个纯粹的JSON结果......我是我目前没有...我期待简单的解决方案在根元素处省略字符串XMLNS,我返回JSON ...结果我得到:

我的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
大家好,我终于找到了删除 xml 命名空间或字符串命名空间的答案。解决方案是使用 context 方法,直接将内容写入浏览器本身。为此,您可以使用此代码。
String result =js.Serialize(data);
this.Context.Response.ContentType = "application/json; charset=utf-8";
this.Context.Response.Write(result);
Run Code Online (Sandbox Code Playgroud)
这会将 json 序列化的原始数据写入浏览器,而无需任何命名空间。请注意,它不能在 Restful api 的上下文中使用,因为 this 可能会有所不同。
| 归档时间: |
|
| 查看次数: |
584 次 |
| 最近记录: |