Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Returning JSON from Web Service
Message
 
To
19/05/2014 13:52:46
General information
Forum:
ASP.NET
Category:
Web Services
Environment versions
Environment:
C# 4.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01600242
Message ID:
01600357
Views:
61
Frank,

You can't really return JSON as a true JSON response from a Web Service. If you pass back a JSON string that string is turned into an XML string. Effectively you pass back a JSON string encoded as an XML result.It's JSON but not hte way a typical client application would expect as it would have to first parse the XML then parse the JSON which is silly.

There are lots of ways to generate real JSON responses in .NET. You can do it manually in any endpoint technology, but Web Services (both ASMX and WCF) don't support this directly. There's ASP.NET AJAX Services that allow you to return JSON from a 'asmx web service' (Called a script service) but then the service also doesn't work with SOAP anymore. You can use WCF REST which allows for both XML and JSON results, but it's not a SOAP server either. You can do it by hand using an HTTP Handler of course.

In short SOAP and JSON and raw XML don't mix becacuse SOAP is basically a higher level protocol than REST based raw data results.

The best solution is to use a framework that supports content negotiation - Web API does and MVC can be made to do this as well (just wrote a blog post about that today in fact).

Hope this helps,

+++ Rick ---


>Hi,
>
>I have a web service that returns my data as XML. How do I get it to return it as json instead?
>
>This works to return XML:
>
>    public CustomerEntity GetCustomerByCustomerNumber(string customerNumber)
>    {
>        // Instantiate the RunSheet business object
>        Customer oCustomer = new Customer();
>
>         return oCustomer.GetCustomerByCustomerNumber(customerNumber);
>}
>
>This is what I get from the above:
>
This XML file does not appear to have any style information associated with it. The document tree is shown below.
><CustomerEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://servername//xmlwebservices/">
><are_name>San Juan</are_name>
><cus_addr1>175 El Socorro Mn Rd</cus_addr1>
><cus_arefk>bf5d7cb4-1d72-4a5d-a279-de5b056a7205</cus_arefk>
><cus_bxtfk>10</cus_bxtfk>
><cus_cashonly>false</cus_cashonly>
><cus_cntfk>e3779ef3-79ab-4a85-91a9-d38117ce7f99</cus_cntfk>
><cus_company>Frank Cazabon</cus_company>
><cus_credit>false</cus_credit>
><cus_customs>false</cus_customs>
><cus_servicefee>true</cus_servicefee>
><cus_cutfk>1</cus_cutfk>
><cus_firstname>Frank</cus_firstname>
><cus_heathrow>false</cus_heathrow>
><cus_hold>false</cus_hold>
><cus_insurance>false</cus_insurance>
><cus_issue>1999-03-29T00:00:00</cus_issue>
><cus_lastname>Cazabon</cus_lastname>
><cus_maifk xsi:nil="true"/>
><cus_miami>true</cus_miami>
><cus_number>0001</cus_number>
><cus_PK>17d0638b-aebf-4d0b-b202-2a4abae0827c</cus_PK>
><cus_renewal>2014-03-29T00:00:00</cus_renewal>
><cus_rtefk>d1a8b4e4-2291-4d8a-95fc-2646b62088e1</cus_rtefk>
><cus_stafk>1</cus_stafk>
><cus_zipfk>6cdb7bdf-9d9b-4441-b884-54921d1cac46</cus_zipfk>
></CustomerEntity>
>
>I tried this, but it doesn't work:
>
    public string GetCustomerByCustomerNumber(string customerNumber)
>    {
>        // Instantiate the RunSheet business object
>        Customer oCustomer = new Customer();
>
>         string json = JsonConvert.SerializeObject(oCustomer.GetCustomerByCustomerNumber(customerNumber), Formatting.Indented);
>        return json;
>}
>
>This is what is returned:
>
>
This XML file does not appear to have any style information associated with it. The document tree is shown below.
><string xmlns="http://servername//xmlwebservices/">{ "$id": "1", "EntityKey": null }</string>
>
>Any ideas on what I need to do?
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform