WebService返回JSON被方法名包裹,如何获取原始JSON数据?
问题:WebService返回的JSON被自动包裹在方法名字段中
我开发了一个供Android应用调用的WebService,Login方法本该直接返回构造好的JSON字符串,但实际返回结果被自动套在了LoginResult字段里。
手动构造的JSON:
{ "UserID": "xxx", "Salutation": "xxx", "Title": "xxx", "Name": "xxx", "Lastname": "xxx" }
实际返回的结果:
{ "LoginResult": { "UserID": "xxx", "Salutation": "xxx", "Title": "xxx", "Name": "xxx", "Lastname": "xxx" }}
当前Login方法代码:
public string Login(string username, string passwort) { string json = "error"; try { var login = Namespace.ViewModels.LoginData.SelectByUserNamePassword(username, passwort); Namespace.Models.WebAppUser webUser = new Namespace.Models.WebAppUser(); lock (webUser) { webUser = new Namespace.Controllers.ApplicationController().setWebUserT(Convert.ToInt32(login.UserID), Convert.ToInt32(login.BetriebID), login.BetriebTyp, login.SessionID, login.UniqueID, login.DBName); } if (webUser != null && webUser.UserID != -1) { json = "{ \"UserID\": \"" + login.UserID + "\", \"Salutation\": \"" + webUser.Salutation + "\", \"Title\": \"" + webUser.Title + "\", \"Name\": \"" + webUser.Name + "\", \"Lastname\": \"" + webUser.Lastname + "\" }"; } } catch (Exception ex) { SimpleLogger.Exception(ex); } return json; }
解决方案
1. 改用返回HttpResponseMessage替代字符串
WebService框架(如ASMX或WCF)默认会包装返回的字符串,直接返回HttpResponseMessage可绕过该逻辑:
public HttpResponseMessage Login(string username, string passwort) { string json = "\"error\""; try { var login = Namespace.ViewModels.LoginData.SelectByUserNamePassword(username, passwort); Namespace.Models.WebAppUser webUser = new Namespace.Models.WebAppUser(); lock (webUser) { webUser = new Namespace.Controllers.ApplicationController().setWebUserT(Convert.ToInt32(login.UserID), Convert.ToInt32(login.BetriebID), login.BetriebTyp, login.SessionID, login.UniqueID, login.DBName); } if (webUser != null && webUser.UserID != -1) { json = "{ \"UserID\": \"" + login.UserID + "\", \"Salutation\": \"" + webUser.Salutation + "\", \"Title\": \"" + webUser.Title + "\", \"Name\": \"" + webUser.Name + "\", \"Lastname\": \"" + webUser.Lastname + "\" }"; } } catch (Exception ex) { SimpleLogger.Exception(ex); } return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(json, Encoding.UTF8, "application/json") }; }
2. 避免手动拼接JSON(推荐优化)
手动拼接JSON易出错,建议用序列化库(如Newtonsoft.Json)直接序列化对象:
// 先定义响应数据类 public class LoginResponse { public string UserID { get; set; } public string Salutation { get; set; } public string Title { get; set; } public string Name { get; set; } public string Lastname { get; set; } } // 修改Login方法中的JSON构造逻辑 if (webUser != null && webUser.UserID != -1) { var response = new LoginResponse { UserID = login.UserID, Salutation = webUser.Salutation, Title = webUser.Title, Name = webUser.Name, Lastname = webUser.Lastname }; json = JsonConvert.SerializeObject(response); }
3. ASMX服务专属配置:直接写入响应流
如果是ASMX服务,可在WebMethod上添加特性,改为直接写入响应流:
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public void Login(string username, string passwort) { string json = "\"error\""; try { // 原有业务逻辑... } catch (Exception ex) { SimpleLogger.Exception(ex); } Context.Response.ContentType = "application/json"; Context.Response.Write(json); }
内容的提问来源于stack exchange,提问作者Mike
相关产品推荐
相关产品推荐

