当前位置: 首页 >>服务端开发 >>Asp.Net >>C#接口开发 >>Webservice返回json数据格式

Webservice返回json数据格式

时间:2018/6/28 12:30:50 【admin】

Webservice返回json数据格式…

问题:我将结果内容用字符串拼接成Json数据并返回的时候,会在结果前面添加xml头部,结果如下。


[plain] view plaincopy

<string xmlns="http://tempuri.org/">  

{"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 ","remark":"","parentId":"B001"},{"batchId":"B004","produceOrderId":"生产订单4", "produceFactory":"测试公司3","addTime":"2014/7/8 ","remark":"","parentId":"B002"},{"batchId":"B005","produceOrderId":"生产订单5", "produceFactory":"测试公司4","addTime":"2014/7/8 ","remark":"","parentId":"B004"},{"batchId":"B006","produceOrderId":"生产订单6", "produceFactory":"外包公司1","addTime":"2014/7/8 ","remark":"","parentId":"B005"}]}  

</string>  

产生如上数据的Webservice接口代码如下。

[plain] view plaincopy

[WebMethod]  

    public String GetQrCodeList(String qrCode)  

    {  

       

        return PriGetQrCodeList(qrCode);  

    }  

  

    /// <summary>  

    /// 获取关联的二维码树结构  

    /// </summary>  

    /// <param name="qrCode">产品二维码</param>  

    /// <returns></returns>  

    private String PriGetQrCodeList(String qrCode)  

    {  

        try  

        {  

            String sfull = "{{\"data\":[{0}]}}";  

            String spart = "{{\"batchId\":\"{0}\",\"produceOrderId\":\"{1}\", \"produceFactory\":\"{2}\",\"addTime\":\"{3}\",\"remark\":\"{4}\",\"parentId\":\"{5}\"}}";  

            String sData = "";  

            DataTable dt = bll.GetQrCodeList(qrCode);  

            if (dt == null || dt.Rows.Count == 0) return "";  

            for (int i = 0; i < dt.Rows.Count; i++)  

            {  

                sData += (String.Format(spart,  

                    dt.Rows[i]["BatchId"].ToString(),  

                    dt.Rows[i]["ProduceOrderId"].ToString(),  

                    dt.Rows[i]["ProduceFactory"].ToString(),  

                    dt.Rows[i]["AddTime"].ToString(),  

                    dt.Rows[i]["Remark"].ToString(),  

                    dt.Rows[i]["ParentId"].ToString()) + ",");  

            }  

            return String.Format(sfull, sData.TrimEnd(','));  

        }  

        catch (Exception)  

        {  

            return "";  

        }  

    }

解决方案:


改变数据的返回方式,用 Context.Response.Write代替return 语句,修改GetQrCodeList方法如下即可返回正确的Json格式数据。

[plain] view plaincopy

<span >    [WebMethod]  

    public void GetQrCodeList(String qrCode)  

    {  

        Context.Response.Charset = "GB2312"; //设置字符集类型  

        Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");  

        Context.Response.Write(PriGetQrCodeList(qrCode));  

        Context.Response.End();  

    }</span>  

返回结果如下:

[plain] view plaincopy


{"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 ","remark":"","parentId":"B001"},{"batchId":"B004","produceOrderId":"生产订单4", "produceFactory":"测试公司3","addTime":"2014/7/8 ","remark":"","parentId":"B002"},{"batchId":"B005","produceOrderId":"生产订单5", "produceFactory":"测试公司4","addTime":"2014/7/8 ","remark":"","parentId":"B004"},{"batchId":"B006","produceOrderId":"生产订单6", "produceFactory":"外包公司1","addTime":"2014/7/8 ","remark":"","parentId":"B005"}]}  


扩充阅读

CopyRight:2007-2018 编程语言吧 备案ICP:湘ICP备09009000号-15 http://www.yuyanba.com

分享按钮