ASP.NET WebMethod 返回用引号括起来的 JSON
我有一个带有 WebMethod 的 asp.net 页面,用于将 JSON 传递回我的 javascript。
下面是 Web 方法:
[WebMethod]
public static string getData(Dictionary<string, string> d) {
string response = "{ \"firstname\": \"John\", \"lastname\": \"Smith\" }";
return response;
}
当它返回到客户端时,其格式如下:
{ \"d\": \"{ \"firstname\": \"John\", \"lastname\": \"Smith\" }\" }
问题是双引号将“d”下的所有内容括起来。我在网络方法或其他返回不带引号的数据的方法中遗漏了一些东西吗?我真的不想每次都把它剥离给客户。我还看过其他文章,但没有发生这种情况。
任何帮助将不胜感激。
I have an asp.net page with a WebMethod on it to pass JSON back to my javascript.
Bellow is the 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 the double quotes wrapping everything under 'd'. Is there something I've missed in the web method or some other means of returning the data without the quotes? I don't really want to be stripping it out on the client everytime. Also I've seen other articles where this doesn't happen.
Any help would be appreciated thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您想要返回对象的 JSON 表示形式,
但您的方法签名返回一个字符串。 ASP.Net 框架序列化正确序列化字符串
response
。换句话说,如果你的函数是,如果输出是,你不会感到惊讶,只是
碰巧
response
有需要转义的双引号。显然你只是想得到这个物体。您有 2 个选项: -
1) 在 javascript 中使用
eval
将字符串转换为对象,例如2) 或(这是我首选的解决方案)让您的方法返回一个实际对象并让 JSON框架的序列化为您完成繁重的工作
您将看到这会生成您最初可能期望的响应(例如
{"d":{"firstname":"John", "lastname":"Smith"}}
I assume that you want to return the JSON representation of the object
but your method signature is returning a string. The ASP.Net framework serialisation is correctly serialising the string
response
. Put another way, if your function wasYou would not be surprised if the output was
It just happens that
response
has double quotes that need to be escaped.You obviously just want to get at the object. You have 2 options: -
1) use
eval
in your javascript to turn the string into an object e.g.2) or (and this is my prefered solution) have your method return an actual object and let the JSON serialisationof the framework do the heavy lifting for you
You will see that this generates the response that you probably originally expected (e.g.
{"d":{"firstname":"John", "lastname":"Smith"}}
实际上,整个问题的存在是因为您试图超越 ASP.Net Web 服务。您需要为要返回的数据设置一个类,并使用该类(或 List(of YourClass))对结果进行排队并返回它们。
一篇很好的文章解释了这一切(一个非常常见的陷阱)是: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
Actually this entire issue exists because you're trying to out-think ASP.Net web services. You need to setup a class for your data to be returned and use that class (or List(of YourClass)) to queue up results and return them.
A great article explaining all this (a very common pitfall) is: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
我的代码也有类似的问题。我试图将 XmlDocument 作为 JSON 返回到调用脚本,但从 WebService 返回 XmlDocument 返回了一组空数组(因为 XmlDocument 不可序列化!)。
如果我使用 ResponseFormat.JSON 属性设置 ScriptService,那么我的 JSON 就会被双重转义。
打败 ASP.NET 的方法是告诉 ASP.NET 你正在返回 XML,然后它不会双重转义你的 JSON ;-)
我知道这是一个 hack,但是......
I had a similar issue with my code. I was trying to return an XmlDocument as JSON to a calling script but returning XmlDocument from the WebService returned an empty set of arrays (as XmlDocument is NOT serializable!).
If i set the ScriptService with the attribute ResponseFormat.JSON then my JSON was double-escaped.
The way to out-fox ASP.NET is to tell ASP.NET that you're returning XML and then it won't double-escape your JSON ;-)
I know it's a hack but .....