当前位置: 首页 > 编程语言 > C#

如何从3.5asmxWebService获取JSON响应分享

时间:2023-04-10 20:15:31 C#

如何从3.5asmxWebService获取JSON响应我有以下方法:使用System.Web.Script.Services;使用系统。Web.Script.序列化;使用Newtonsoft.Json;使用系统集合;[WebService(Namespace="http://tempuri.org/")][WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]//[System.ComponentModel。ToolboxItem(false)][System.Web.Script.Services.ScriptService]//[System.Web.Script.Services.ScriptService]publicclassTripadvisor:System.Web.Services.WebService{publicTripadvisor(){//取消注释如果使用设计的组件,则下一行//InitializeComponent();}[WebMethod][ScriptMethod(ResponseFormat=ResponseFormat.Json)]publicstringHotelAvailability(stringapi){JavaScriptSerializerjs=newJavaScriptSerializer();字符串json=js.Serialize(api);//JsonConvert.SerializeObject(api);返回json;在这里,我设置了ResponseFormat属性,以便json仍作为XML返回。我想以json格式使用这个asmx服务有什么想法吗?我遇到了同样的问题,并包含以下代码以使其工作。[WebMethod][ScriptMethod(UseHttpGet=true,ResponseFormat=ResponseFormat.Json)]publicvoidHelloWorld(){Context.Response.Clear();Context.Response.ContentType="应用程序/json";Context.Response.Write("你好世界");//返回“你好世界”;更新:要获得纯json格式,您可以使用如下所示的javascript序列化程序。publicclassWebService1:System.Web.Services.WebService{[WebMethod][ScriptMethod(UseHttpGet=true,ResponseFormat=ResponseFormat.Json)]publicvoidHelloWorld(){JavaScriptSerializerjs=newJavaScriptSerializer();上下文.Response.Clear();Context.Response.ContentType="应用程序/json";HelloWorldData数据=newHelloWorldData();data.Message="你好世界";上下文.Response.Write(js.Serialize(data));}}publicclassHelloWorldData{publicStringMessage;但这适用于复杂类型,但String没有显示任何差异。就一个问题。你什么时候没有收到JSON响应?因为当您从客户端调用Web服务时(我假设是Web浏览器,即xhr),您应该在请求中将内容类型标头指定为“application/json;charset=yourcharset”。我相信无论从客户端指定什么内容类型,上述解决方案总是返回json。dotnetasmx框架允许内容类型标头方法,因此当有简洁的解决方案可用时,上述内容可以归类为hack。关于从ASMX网络服务返回Json数据的类似问题这可能有帮助->http://forums.asp.net/p/1054378/2338982.aspx#2338982PS:我假设您使用的是dotnet版本4。亲爱的未来读者:当前接受的答案不是正确的方法。它将您绑定到使用JavaScriptSerializer并且您失去了请求xml(或者您可能拥有的任何序列化格式)的能力。“正确的方法”还涉及更少的代码!如果您使用[ScriptService]属性(您拥有)装饰您的服务类,那么每当您的Ajax调用请求JSON时,ASP.NET3.5+将自动将响应序列化为JSON。手动序列化为JSON的建议是完全错误的,除非您希望使用其他序列化程序,例如Newtonsoft。您会看到XML指示以下内容之一:这是一个支持JSON的ASMXWeb服务的简单工作示例:usingSystem;使用System.Collections.Generic;使用System.Web.Services;[WebService(Namespace="http://tempuri.org/")][WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService]publicclassWebService:System.Web.Services.WebService{[WebMethod]publicMyClassExample(){returnnew}publicclassMyClass{publicstringMessage{get{return"Hi";}}publicintNumber{得到{返回123;}}publicListList{get{returnnewList{"Item1","Item2","Item3"};}}}}JavaScript请求它并处理响应(我们只是弹出一个带有来自MyClass.Message的消息的JS警报):测试Http请求:POSThttp://HOST.com/WebService.asmx/ExampleHTTP/1.1Accept:应用程序/json,文本/javascript,*/*;q=0.01内容类型:application/json;charset=utf-8X-Requested-With:XMLHttpRequestReferer:http://HOST.com/Test.aspxAccept-Language:en-GB,en;q=0.5Accept-Encoding:gzip,deflate使用r-Agent:Mozilla/5.0(compatible;MSIE10.0;WindowsNT6.2;WOW64;Trident/6.0)Connection:Keep-AliveContent-Length:3Host:HOST.com{}HTTPResponse:HTTP/1.1200OKCache-控制:私有,max-age=0内容类型:application/json;charset=utf-8服务器:Microsoft-IIS/8.0X-AspNet-版本:4.0.30319X-Powered-By:ASP.NET日期:星期二,2018年2月20日08:36:12GMT内容长度:98{"d":{"__type":"WebService+MyClass","Message":"Hi","Number":123,"List":["Item1","Item2","Item3"]}}结果:"JS弹窗显示“Hi”以上是C#学习教程:HowtogettheJSONresponsefromthe3.5asmxwebservice很有用,需要多了解C#学习教程,希望大家多多关注it——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: