使用 [WebMethod] 转义 JSON 响应

发布于 2024-07-17 18:54:26 字数 1193 浏览 2 评论 0原文

以下代码的 JSON 响应被错误地转义,如下所述。

我的 webmethod 是这样的:

    [WebMethod (Description="doc here")]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)] 
    public string responseMyObject() {
            if (!Setup()) return "";

            ...
            Proxy pu = new Proxy(...);
...

            string toReturn = JavaScriptConvert.SerializeObject(pu.getMyObject());
            Console.WriteLine(toReturn);
            return toReturn;
    }

从控制台我得到:

{"field1":vaule1,"field2":value2}

from JS:

$.ajax({
    type: "POST",
    url: "/myapi/myClass.asmx/responseMyObject",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
                    var object = msg.d;
                    alert(object.field1);
    }
});

问题是在 HTTP 响应头中我可以看到 JSON 响应错误地(?)通过以下方式转义:

{"d":"{\"field1\":value1,\"field2\":value2}"}

奇怪的是控制台打印是很好(但尚未封装在 {d: ...} 中)

{"field1":value1,"field2":value2}

使用类似的代码,如果我调用返回基本类型(无对象)的 [WebMethod],JSON 响应就可以了。例如:

{"d":8080}

The JSON response from the following code is wrongly escaped as described below.

My webmethod is like this:

    [WebMethod (Description="doc here")]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)] 
    public string responseMyObject() {
            if (!Setup()) return "";

            ...
            Proxy pu = new Proxy(...);
...

            string toReturn = JavaScriptConvert.SerializeObject(pu.getMyObject());
            Console.WriteLine(toReturn);
            return toReturn;
    }

from the console I get:

{"field1":vaule1,"field2":value2}

from JS:

$.ajax({
    type: "POST",
    url: "/myapi/myClass.asmx/responseMyObject",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
                    var object = msg.d;
                    alert(object.field1);
    }
});

The problem is that in the HTTP response header I can see that the JSON response is wrongly (?) escaped in the following way:

{"d":"{\"field1\":value1,\"field2\":value2}"}

What's strange is that the console print is fine (but not yet encapsulated in {d: ...}

{"field1":value1,"field2":value2}

With similar code, if I call a [WebMethod] that returns basic types (no object) the JSON response is ok. Like:

{"d":8080}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

荆棘i 2024-07-24 18:54:26
[WebService]
[ScriptService]
public class MyWebService : WebService
{    

  [WebMethod (Description="doc here")]    
  [ScriptMethod( UseHttpGet=false, ResponseFormat=ResponseFormat.Json)]     
  public MyObjectType responseMyObject() 
  {
      Proxy pu = new Proxy(...);

      return pu.GetMyObject();
  }

}

您不需要 JSON 序列化器,使用 ScriptService 属性对其进行标记即可使其具有序列化 JSON 的能力。 您预先序列化 JSON,然后再次序列化它:(

[WebService]
[ScriptService]
public class MyWebService : WebService
{    

  [WebMethod (Description="doc here")]    
  [ScriptMethod( UseHttpGet=false, ResponseFormat=ResponseFormat.Json)]     
  public MyObjectType responseMyObject() 
  {
      Proxy pu = new Proxy(...);

      return pu.GetMyObject();
  }

}

You dont need a JSON serializer, tagging it with the ScriptService attribute gives it tie ability to serialize JSON out. You were pre serializing the JSON and then serializing it again :(

狼亦尘 2024-07-24 18:54:26

为什么要调用 JavaScriptConvert.SerializeObject?

难道您不能只将方法的返回类型更改为 pu.getMyObject() 返回的类型,然后框架将完成其余的工作吗?

换句话说......

[WebMethod (Description="doc here")]    
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]     
public MyObjectType responseMyObject() 
{
    Proxy pu = new Proxy(...);

    ...

    return pu.GetMyObject();
}

目前我认为您正在将对象序列化为 JSON 格式,然后,当您从该方法返回时,框架将该字符串(包含 JSON 格式的数据)序列化为 JSON 格式。

Why are you calling JavaScriptConvert.SerializeObject?

Can't you just change the return type of your method to be the type returned by pu.getMyObject() and the framework will do the rest?

In other words...

[WebMethod (Description="doc here")]    
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]     
public MyObjectType responseMyObject() 
{
    Proxy pu = new Proxy(...);

    ...

    return pu.GetMyObject();
}

At the moment I think you're serializing your object into a JSON format and then, when you return from the method, the framework is serializing that string (which contains JSON formatted data) into a JSON format.

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