ASP.NET WebMethod 返回用引号括起来的 JSON

发布于 2024-09-04 22:46:59 字数 525 浏览 1 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

池予 2024-09-11 22:46:59

我假设您想要返回对象的 JSON 表示形式,

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

但您的方法签名返回一个字符串。 ASP.Net 框架序列化正确序列化字符串response。换句话说,如果你的函数是,

string response = "foo";
return response; 

如果输出是,你不会感到惊讶,只是

{"d":{"foo"}}

碰巧 response 有需要转义的双引号。

显然你只是想得到这个物体。您有 2 个选项: -

1) 在 javascript 中使用 eval 将字符串转换为对象,例如

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

2) 或(这是我首选的解决方案)让您的方法返回一个实际对象并让 JSON框架的序列化为您完成繁重的工作

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

您将看到这会生成您最初可能期望的响应(例如
{"d":{"firstname":"John", "lastname":"Smith"}}

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

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

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 was

string response = "foo";
return response; 

You would not be surprised if the output was

{"d":{"foo"}}

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.

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

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

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

You will see that this generates the response that you probably originally expected (e.g.
{"d":{"firstname":"John", "lastname":"Smith"}}

折戟 2024-09-11 22:46:59

实际上,整个问题的存在是因为您试图超越 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/

看春风乍起 2024-09-11 22:46:59

我的代码也有类似的问题。我试图将 XmlDocument 作为 JSON 返回到调用脚本,但从 WebService 返回 XmlDocument 返回了一组空数组(因为 XmlDocument 不可序列化!)。

如果我使用 ResponseFormat.JSON 属性设置 ScriptService,那么我的 JSON 就会被双重转义。

打败 ASP.NET 的方法是告诉 ASP.NET 你正在返回 XML,然后它不会双重转义你的 JSON ;-)

    [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;
    }

我知道这是一个 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 ;-)

    [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 it's a hack but .....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文