ASP.NET WebMethod returns JSON wrapped in quotation marks

I have an asp.net page with WebMethod to pass JSON back to my javascript.

Bellow is a web method:

[WebMethod] public static string getData(Dictionary<string, string> d) { string response = "{ \"firstname\": \"John\", \"lastname\": \"Smith\" }"; return response; } 

When this is returned to the client, it is formatted as follows:

 { \"d\": \"{ \"firstname\": \"John\", \"lastname\": \"Smith\" }\" } 

The problem is double quotes, which wrap everything under "d". Is there something I missed in the web method or some other ways to return the data without quotes? I do not want to delete it to the client every time. I also saw other articles in which this does not happen.

Any help would be greatly appreciated.

+7
json asmx
source share
3 answers

I assume you want to return a JSON representation of the object

  { firstname:"John", lastname:"Smith" } 

but your method signature returns a string. Serializing the ASP.Net structure correctly , serializing the response string. In other words, if your function was

 string response = "foo"; return response; 

No wonder if the result was

 {"d":{"foo"}} 

It just happens that response has double quotes that need to be escaped.

You obviously just want to hit the object. You have 2 options: -

1) use eval in your javascript to turn a string into an object, e.g.

 function onSuccessCallback(retval) { var obj = eval(retval.d); }` 

2) or (and this is my preferred solution), your method returns a real object, and let serializing the JSON framework make a heavy lift for you

 [WebMethod] public static object getData(Dictionary<string, string> d) { var response = new { firstname = "John", lastname="Smith" }; return response; } 

You will see that this generates a response that you probably originally expected (for example, {"d":{"firstname":"John", "lastname":"Smith"}}

+8
source share

In fact, this whole problem exists because you are trying to redefine ASP.Net web services. You need to configure the class for the returned data and use this class (or List (of YourClass)) to queue the results and return them.

Great article explaining all of this (a very common mistake): http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

+3
source share

I had a similar problem with my code. I tried to return the XmlDocument as JSON to the calling script, but returning the XmlDocument from the WebService returned an empty array set (since the XmlDocument is NOT serializable!).

If I set ScriptService with the ResponseFormat.JSON attribute, then my JSON was double-escaped.

The path to out-fox ASP.NET is to tell ASP.NET that you are returning XML, and then it will not avoid your JSON twice; -)

  [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Xml)] public String MyJSONWebService(String sParam1, String sParam2, String sParam3) { ... do stuff..... XmlDocument oXMLDocument = new XmlDocument(); oXMLDocument.LoadXml(sXML); sJSON = JsonConvert.SerializeXmlNode(oXMLDocument.SelectSingleNode("autnresponse")); return sJSON; } 

I know this is a hack, but .....

-one
source share

All Articles