I just found this thread while looking for a solution for this problem and wanted to add a solution I found in case someone else is searching for an answer too.
I am using .net 3.5 with VB I was was trying to return an entity from a webmethod in a web service, and was getting an error when it was trying to serialize to json.
I tried converting it to a json string and returning it, using System.Web.Script.Serialization.JavaScriptSerializer, which worked,
but the client just got the json string as a string, and i wanted the results to be an object.
I would have tried Josh's stuff from his blog however i didn't want to convert all of his code to vb, it wasn't immediately clear to me how it worked since it returned a string, not sure if it would be Content-Type: application/json on the client side or just a string, like my other attempts. One of the comments on his blog from "NATHAN" pointed me to my solution.
So I changed my return type from Entity to Object and returned an anonymous type with just the fields i wanted.
I usually don't like anything that returns an Object but since it is only being called by JS i think this should be acceptable since it will be an object on the client side.
<WebMethod()> _
Public Function GetPortalItem(ByVal portalItemId As Integer) As Object
Dim item = PortalItemManager.FetchByPortalItemID(portalItemId)
'hack to return an anon type via json with just the data we want
Return New With {.Title = item.Title, .PortalItemID = item.PortalItemId}
End Function
I just let the magic of asp.net convert it for me to an object or actually a json string, and since it is doing the work it also knows to set the content type to json too. On the client side it is a javascript Object ready to use.
Gabe