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.        
  5.         return PriGetQrCodeList(qrCode);  
  6.     }  
  7.   
  8.     /// <summary>  
  9.     /// 获取关联的二维码树结构  
  10.     /// </summary>  
  11.     /// <param name="qrCode">产品二维码</param>  
  12.     /// <returns></returns>  
  13.     private String PriGetQrCodeList(String qrCode)  
  14.     {  
  15.         try  
  16.         {  
  17.             String sfull = "{{\"data\":[{0}]}}";  
  18.             String spart = "{{\"batchId\":\"{0}\",\"produceOrderId\":\"{1}\", \"produceFactory\":\"{2}\",\"addTime\":\"{3}\",\"remark\":\"{4}\",\"parentId\":\"{5}\"}}";  
  19.             String sData = "";  
  20.             DataTable dt = bll.GetQrCodeList(qrCode);  
  21.             if (dt == null || dt.Rows.Count == 0) return "";  
  22.             for (int i = 0; i < dt.Rows.Count; i++)  
  23.             {  
  24.                 sData += (String.Format(spart,  
  25.                     dt.Rows[i]["BatchId"].ToString(),  
  26.                     dt.Rows[i]["ProduceOrderId"].ToString(),  
  27.                     dt.Rows[i]["ProduceFactory"].ToString(),  
  28.                     dt.Rows[i]["AddTime"].ToString(),  
  29.                     dt.Rows[i]["Remark"].ToString(),  
  30.                     dt.Rows[i]["ParentId"].ToString()) + ",");  
  31.             }  
  32.             return String.Format(sfull, sData.TrimEnd(','));  
  33.         }  
  34.         catch (Exception)  
  35.         {  
  36.             return "";  
  37.         }  
  38.     }</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>  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值