1. 程式人生 > >Webservice返回json資料格式不帶xml頭部

Webservice返回json資料格式不帶xml頭部

我將結果內容用字串拼接成Json資料並返回的時候,會在結果前面新增xml頭部,結果如下。

  1. <span ><string xmlns="http://tempuri.org/">  
  2. {"data":[{"batchId":"B001","produceOrderId":"", "produceFactory":"","addTime":"2014/7/8 15:09:00","remark":"","parentId":""},{"batchId":"B002","produceOrderId":"生產訂單2", "produceFactory":"測試公司1","addTime":"2014/7/8 8:00:00","remark":"","parentId":"B001"},{"batchId":"B003","produceOrderId":"生產訂單3", "produceFactory":"測試公司2","addTime":"2014/7/8 0:00:00","remark":"","parentId":"B001"},{"batchId":"B004","produceOrderId":"生產訂單4", "produceFactory":"測試公司3","addTime":"2014/7/8 0:00:00","remark":"","parentId":"B002"},{"batchId":"B005","produceOrderId":"生產訂單5", "produceFactory":"測試公司4","addTime":"2014/7/8 0:00:00","remark":"","parentId":"B004"},{"batchId":"B006","produceOrderId":"生產訂單6", "produceFactory":"外包公司1","addTime":"2014/7/8 0:00:00","remark":"","parentId":"B005"}]}  
  3. </string></span>  

產生如上資料的Webservice介面程式碼如下。

  1. <span >  [WebMethod]  
  2.     public String GetQrCodeList(String qrCode)  
  3.     {  
  4.         return PriGetQrCodeList(qrCode);  
  5.     }  
  6.     /// <summary>  
  7.     /// 獲取關聯的二維碼樹結構  
  8.     /// </summary>  
  9.     /// <param name="qrCode">產品二維碼</param>  
  10.     /// <returns></returns>  
  11.     private String PriGetQrCodeList(String qrCode)  
  12.     {  
  13.         try  
  14.         {  
  15.             String sfull = "{{\"data\":[{0}]}}";  
  16.             String spart = "{{\"batchId\":\"{0}\",\"produceOrderId\":\"{1}\", \"produceFactory\":\"{2}\",\"addTime\":\"{3}\",\"remark\":\"{4}\",\"parentId\":\"{5}\"}}";  
  17.             String sData = "";  
  18.             DataTable dt = bll.GetQrCodeList(qrCode);  
  19.             if (dt == null || dt.Rows.Count == 0) return "";  
  20.             for (int i = 0; i < dt.Rows.Count; i++)  
  21.             {  
  22.                 sData += (String.Format(spart,  
  23.                     dt.Rows[i]["BatchId"].ToString(),  
  24.                     dt.Rows[i]["ProduceOrderId"].ToString(),  
  25.                     dt.Rows[i]["ProduceFactory"].ToString(),  
  26.                     dt.Rows[i]["AddTime"].ToString(),  
  27.                     dt.Rows[i]["Remark"].ToString(),  
  28.                     dt.Rows[i]["ParentId"].ToString()) + ",");  
  29.             }  
  30.             return String.Format(sfull, sData.TrimEnd(','));  
  31.         }  
  32.         catch (Exception)  
  33.         {  
  34.             return "";  
  35.         }  
  36.     }</span>  

解決方案:

改變資料的返回方式,用 Context.Response.Write代替return 語句,修改GetQrCodeList方法如下即可返回正確的Json格式資料。

  1. <span >    [WebMethod]  
  2.     public void GetQrCodeList(String qrCode)  
  3.     {  
  4.         Context.Response.Charset = "GB2312"; //設定字符集型別  
  5.         Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");  
  6.         Context.Response.Write(PriGetQrCodeList(qrCode));  
  7.         Context.Response.End();  
  8.     }</span>  

返回結果如下:

  1. <span >{"data":[{"batchId":"B001","produceOrderId":"", "produceFactory":"","addTime":"2014/7/8 15:09:00","remark":"","parentId":""},{"batchId":"B002","produceOrderId":"生產訂單2", "produceFactory":"測試公司1","addTime":"2014/7/8 8:00:00","remark":"","parentId":"B001"},{"batchId":"B003","produceOrderId":"生產訂單3", "produceFactory":"測試公司2","addTime":"2014/7/8 0:00:00","remark":"","parentId":"B001"},{"batchId":"B004","produceOrderId":"生產訂單4", "produceFactory":"測試公司3","addTime":"2014/7/8 0:00:00","remark":"","parentId":"B002"},{"batchId":"B005","produceOrderId":"生產訂單5", "produceFactory":"測試公司4","addTime":"2014/7/8 0:00:00","remark":"","parentId":"B004"},{"batchId":"B006","produceOrderId":"生產訂單6", "produceFactory":"外包公司1","addTime":"2014/7/8 0:00:00","remark":"","parentId":"B005"}]}</span>