我不确定我在哪里出错或者我漏掉了什么。
我正在构建一个 ASP.NET 2.0(基于 .Net 3.5 框架)Web 应用程序,并且我正在包含一个 Web服务。请注意,这 不是 一个 MVC 项目。我希望公开一个方法,该方法将返回一个 JSON 字符串; 格式化以供 jqGrid jQuery 插件使用。
这是我在服务中实现的初步测试方法:感谢 (Phil Haack's Guide for 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();
}
被调用时,它返回(为了清晰起见格式化后):
<?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>
如何在没有xml包装的情况下实现上述响应?