Return JSON from ASP.NET (VB) webservice

4,830 views
Skip to first unread message

jtaylor

unread,
Dec 2, 2011, 10:40:48 PM12/2/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I have a ASP.NET webservice written in VB using VS2010. I need it to
return JSON but it insists on returning XML. I set
"<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>" on the function,
but it still returns XML.

Any suggestions on where to start looking?

Cerebrus

unread,
Dec 5, 2011, 8:42:15 AM12/5/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
1. Show us the entire WebMethod.

2. Tell us the content type header being sent by the requesting
client.

oldwolf

unread,
Dec 5, 2011, 10:42:56 PM12/5/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
using Newtonsoft.Json;
....
DefineClass yourClass = new DefineClass();
/* process yourClass Data */
string strJsonReturn = JsonConvert.SerializeObject(yourClass,
Formatting.Indented);
Response.ContentType = "application/x-javascript";
Response.Write(strJsonReturn);

it will return JSON

Sorry because this code is C# but you can change to VB.NET easy.

jtaylor

unread,
Dec 10, 2011, 10:59:19 AM12/10/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
So you're just serializing the object into a string as JSON? I will
give that a shot and see what happens.

jtaylor

unread,
Dec 10, 2011, 10:57:47 AM12/10/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
#1:
<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function myWsFunction(ByVal parms As String) As DataTable

Return CreateDataTable(parms)
End Function

#2. I'm just testing it in VS.

Cerebrus

unread,
Dec 12, 2011, 6:36:27 AM12/12/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
IMHO, the return type of the function should be a string. JSON and
XML, they're both just strings.

> > > Any suggestions on where to start looking?- Hide quoted text -
>
> - Show quoted text -

jtaylor

unread,
Dec 13, 2011, 8:46:21 PM12/13/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Fair enough.

I am trying to serialize the DataTable into a String as JSON. Here's
the code:

Using memStream As New MemoryStream
Dim serializer As New
DataContractJsonSerializer(lTable.GetType)
serializer.WriteObject(memStream, lTable)
Dim bytes As Byte() = memStream.GetBuffer()
returnValue = Encoding.UTF8.GetString(bytes, 0,
bytes.Length).Trim
End Using


I now have two problems.
1. The String returnValue contains XML, not JSON.
2. My web method appears to be returning the String as an XML document
containing a single string value that contains the text of
returnValue.

jtaylor

unread,
Dec 14, 2011, 11:20:22 PM12/14/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
OK, I figured it would be easy to manually serialize the DataTable
into JSON, and it was. I exposed the webservice to the consumer, and
they said it would work fine if it was not encapsulated in XML. So my
only issue now is #2 from above.

Thanks

Chikelue Oji

unread,
Dec 15, 2011, 4:45:03 AM12/15/11
to dotnetde...@googlegroups.com
Hello Jtaylor,

The easiest way I know to implement this is using Jayrock api/library.
You can get more information by googling Jayrock.

One sacrifice you may have to make is to rewrite and rebuild your webservice using Jayrock's template which I believe is implemented as a custom http handler. They provide a very simple example that will get you up and running quickly and it is not really complicated to use and won't be much work if the web service code you will need to rewrite is not enormous.

I hope this becomes of help to you. Please let us all know if it is.

Cheers.

Chike

From: jtaylor <jta...@lorencook.com>
To: "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting" <dotnetde...@googlegroups.com>
Sent: Thursday, December 15, 2011 5:20 AM
Subject: [DotNetDevelopment] Re: Return JSON from ASP.NET (VB) webservice
--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to dotnetde...@googlegroups.com
To unsubscribe from this group, send email to
dotnetdevelopment+unsub...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net


jtaylor

unread,
Dec 19, 2011, 9:08:58 AM12/19/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I got my webservice to return the text I wanted by instantiating an
HttpContext object and using the .ContentType and .Write methods. Any
reason not to just stick with that?

On Dec 15, 3:45 am, Chikelue Oji <chike_...@yahoo.com> wrote:
> Hello Jtaylor,
>
> The easiest way I know to implement this is using Jayrock api/library.
> You can get more information by googling Jayrock.
>
> One sacrifice you may have to make is to rewrite and rebuild your webservice using Jayrock's template which I believe is implemented as a custom http handler. They provide a very simple example that will get you up and running quickly and it is not really complicated to use and won't be much work if the web service code you will need to rewrite is not enormous.
>
> I hope this becomes of help to you. Please let us all know if it is.
>
> Cheers.
>
> Chike
>
> ________________________________

>  From: jtaylor <jtay...@lorencook.com>

> dotnetdevelopm...@googlegroups.com
> For more options, visit this group athttp://groups.google.com/group/dotnetdevelopment?hl=en?hl=en

Cerebrus

unread,
Dec 20, 2011, 6:39:11 AM12/20/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I think you might have actually accessed the HttpContext object
(Context property) instead of instantiating it. You already have the
best solution, IMHO.

jtaylor

unread,
Dec 20, 2011, 1:14:32 PM12/20/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Here's my new web method:
<WebMethod()>
Public Sub myWsSub(ByVal parms As String)
Dim context as New HttpContext(HttpContext.Current.Request,
HttpContext.Current.Response)
context.Response.ContentType = "application/json;
charset=utf-8"
context.Response.Write(tableToJson(CreateDataTable(parms)))
End Function

Isn't that instantiating a new HttpContext object? Is that not the
right way to do it?

Cerebrus

unread,
Dec 24, 2011, 1:22:06 AM12/24/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Dim context as HttpContext = Me.Context

You don't even need to use a variable here... the HttpContext is
always accessible through the 'Context' property of the Webservice
class.

;-)
> > > reason not to just stick with that?- Hide quoted text -

jtaylor

unread,
Dec 27, 2011, 1:06:14 PM12/27/11
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Me.Context worked!

Thanks again
Reply all
Reply to author
Forward
0 new messages