Home >Backend Development >C++ >How to Convert ASMX Web Service XML Output to Pure JSON?

How to Convert ASMX Web Service XML Output to Pure JSON?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-15 11:48:42561browse

How to Convert ASMX Web Service XML Output to Pure JSON?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn