Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Returning JSON from Web Service
Message
From
20/05/2014 12:09:46
 
 
To
20/05/2014 09:12:20
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:
01600327
Views:
49
>>>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?
>>
>>It may be as simple as using your original method. Assuming you are viewing the results in the browser, the browser's default Accept header usually has application/xml as one of the preferred formats. Try using jquery's getJSON method (http://api.jquery.com/jquery.getjson/) to retrieve your results. That will specifically request the results to be JSON.
>>
>>If that doesn't work, it might be helpful to know what technology you are using for your web service (asmx pages, WCF, Web API) and what version/approach you are using for Entity Framework (Code First, Model First, Database First).
>
>
>Rob,
>
>I tried using getjson (but I'm not sure I tried it correctly as I'm very new to this stuff) but it returned nothing.
>
>I basically added it into another function call in my controller.js and stepped through the code but it never went into the loop to display the data (ie never hit the var items =... line). I did it like this:
>
>
	function taskCountChanged() {
>		var count = $(taskPage).find( '#tblTasks tbody tr').length;
>		$('footer').find('#taskCount').text(count);
>		
>		$.getJSON( "http://localhost:54632/RunSheet%20Web%20Service/RunSheetWS.asmx?op=GetCustomerByCustomerNumber", function( data ) {
>		  var items = [];
>		  $.each( data, function( key, val ) {
>			items.push( "<li id='" + key + "'>" + val + "</li>" );
>		  });
>		 
>		  $( "<ul/>", {
>			"class": "my-new-list",
>			html: items.join( "" )
>		  }).appendTo( "body" );
>		});
>     }
>
>The URL I passed in was the URL which was displayed when I ran my web service in Visual Studio (and I ran it at the same time I tried my json code.
>
>I'm trying my best to read up on and understand all these new technologies at the same time, so be gentle with me :)

I'd suggest looking at the network traffic from dev tools - see what's actually going (and coming) over the wire.
Previous
Reply
Map
View

Click here to load this message in the networking platform