ASP.NET WebService使用XML标记包装我的JSON响应

Mik*_*ike 12 c# asp.net json web-services javascriptserializer

我不确定我错过了什么,我错过了什么.

我正在构建一个ASP.NET 2.0(在.Net 3.5框架上)Web应用程序,我正在包含一个Web服务.请注意,这不是 MVC项目.我希望公开一个返回JSON字符串的方法; 格式化以提供jqGrid jQuery插件.

这是我在我的服务中实现的初步测试方法:感谢(Phil Haack的MVC指南)

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getData()
{
    JavaScriptSerializer ser = new JavaScriptSerializer();

    var jsonData = new
    {
        total = 1, // we'll implement later 
        page = 1,
        records = 3, // implement later 
        rows = new[]{
          new {id = 1, cell = new[] {"1", "-7", "Is this a good question?", "yay"}},
          new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?", "yay"}},
          new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?", "yay"}}
        }
    };

    return ser.Serialize(jsonData); //products.ToString();
}
Run Code Online (Sandbox Code Playgroud)

调用时返回(格式化为清晰):

<?xml version="1.0" encoding="utf-8" ?> 
<string  mlns="http://tempuri.org/">
{
  "total":1,
  "page":1,
  "records":3,
  "rows":
    [
      {"id":1,"cell":["1","-7","Is this a good question?","yay"]},
      {"id":2,"cell":["2","15","Is this a blatant ripoff?","yay"]},
      {"id":3,"cell":["3","23","Why is the sky blue?","yay"]}
    ]
}
</string> 
Run Code Online (Sandbox Code Playgroud)

如果没有 xml包装,我将如何实现上述响应?

Pat*_*her 9

在你的代码中,不要"返回"json.改为使用:

Context.Response.Write(ser.Serialize(jsonData));

然后你会好起来的.

常规返回命令可以通过提供更合适的服务格式来帮助您.有些人会说使用它是更好的形式,并从这种格式打开客户端上的json.我说,只是吐出你想要使用它的东西!


Chr*_*s S 8

你可能不会做的三件事:

  • 将方法标记为静态
  • 执行POST
  • 为jQuery中的数据提供一个空的"{}".

可能有一种方法用GET调用该方法,我只使用过POST.我能够让你的例子使用这个:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
    // In your javascript block
    $(document).ready(function()
    {
        $.ajax({
            url: "/Default.aspx/Tester",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{}",
            success: done
        });
    });

    function done(data)
    {
        // Include http://www.json.org/json2.js if your browser doesn't support JSON natively
        var data = JSON.parse(data.d);
        alert(data.total);
    }
</script>
Run Code Online (Sandbox Code Playgroud)

背后的代码(您不需要创建Web服务,您可以将其放在default.aspx中):

[WebMethod]
public static string Tester()
{
    JavaScriptSerializer ser = new JavaScriptSerializer();

    var jsonData = new
    {
        total = 1, // we'll implement later 
        page = 1,
        records = 3, // implement later 
        rows = new[]{
              new {id = 1, cell = new[] {"1", "-7", "Is this a good question?", "yay"}},
              new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?", "yay"}},
              new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?", "yay"}}
            }
        };

    return ser.Serialize(jsonData); //products.ToString();
}
Run Code Online (Sandbox Code Playgroud)

结果:

{"d":"{\"total\":1,\"page\":1,\"records\":3,\"rows\":[{\"id\":1,\"cell\":[\"1\",\"-7\",\"Is this a good question?\",\"yay\"]},{\"id\":2,\"cell\":[\"2\",\"15\",\"Is this a blatant ripoff?\",\"yay\"]},{\"id\":3,\"cell\":[\"3\",\"23\",\"Why is the sky blue?\",\"yay\"]}]}"}
Run Code Online (Sandbox Code Playgroud)

这里有更详细的解释

  • "d"不是由jQuery添加的,而是.NET 3.5作为安全措施. (2认同)