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
Release: 2025-01-15 11:48:42
Original
565 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template