zoukankan      html  css  js  c++  java
  • 实现WebService只返还json结构数据

      现在项目要求跨平台,数据共享,json结构的数据结果必不可少。而其中跨平台自然用到webservice技术,根据webservice特性传送数据为XML结构,如果各平台使用不可避免要进行XML转换为json数据,往往较为麻烦,为后面日常维护带来不便。能不能使得各个平台调用webservice时候直接返还json数据呢,经过研究发现,是可行的,技术实现如下(以C#做webservice为例)

    通常webservice 接口定义

      [WebMethod]
            public string GetName()
            {
                return "Hello World Produce";
            }

    返还

        

    <?xml version="1.0" encoding="utf-8" ?>
      <string xmlns="http://tempuri.org/">Hello World Produce</string>

    其实 问题就出现在 return 语句上,经过改造,写一个静态方法用来替换 return 
     public static class WebServiceContext
        {
            public static void GetJsonData(System.Web.Services.WebService wb,string jsonData)
            {
               //System.Web.Services.WebService wb = new System.Web.Services.WebService();
                wb.Context.Response.Charset = "utf-8";
                wb.Context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                wb.Context.Response.Write(jsonData);
                wb.Context.Response.End();
            }
        }
    这样返还结果就直接是json数据了。

  • 相关阅读:
    嵌入式整体框架——总结
    DSP Bios记忆
    三遥
    usb设备 配置 接口 端点
    ARM, MIPS, Power PC的比较
    STM32 IAP
    FSMC 总结
    BCD码与十进制的相互转换
    读 “cortexM3” 权威指南 小记(一)
    crc校验码的16 32位 查表法 算法记载
  • 原文地址:https://www.cnblogs.com/zoujinhua/p/12916756.html
Copyright © 2011-2022 走看看