Convert ASMX file output to JSON
Question:
Despite using the ResponseFormat configuration, the output generated by the ASMX web service is still in XML instead of the required JSON format.
Code-behind:
<code class="language-csharp">[System.Web.Script.Services.ScriptService] public class _default : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)] public string[] UserDetails() { return new string[] { "abc", "def" }; } }</code>
Solution:
To output plain JSON without XML wrapping, modify the code as follows:
Code-behind:
<code class="language-csharp">[System.Web.Script.Services.ScriptService] public class _default : System.Web.Services.WebService { [WebMethod] public void UserDetails() { HttpContext.Current.Response.Write("{property: value}"); } }</code>
Instructions:
Change the WebMethod's return type to void and use the Response.Write method to write the JSON string directly to the HttpResponse. This method provides a plain JSON response without XML wrapping.
The above is the detailed content of How to Convert ASMX Web Service XML Output to Pure JSON?. For more information, please follow other related articles on the PHP Chinese website!