【发布时间】:2011-11-27 06:04:17
【问题描述】:
这是我的困境。我正在使用一个 RESTful ASP.NET 服务,试图让一个函数以这种格式返回一个 JSON 字符串:
{"Test1Key":"Test1Value","Test2Key":"Test2Value","Test3Key":"Test3Value"}
但我得到的是这种格式:
[{"Key":"Test1Key","Value":"Test1Value"},
{"Key":"Test2Key","Value":"Test2Value"},
{"Key":"Test3Key","Value":"Test3Value"}]
我的方法是这样的:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Dictionary<string, string> Test(String Token)
{
if (!IsAuthorized(Token))
return null;
if (!IsSecure(HttpContext.Current))
return null;
Dictionary<string, string> testresults = new Dictionary<string, string>();
testresults.Add("Test1Key", "Test1Value");
testresults.Add("Test2Key", "Test2Value");
testresults.Add("Test3Key", "Test3Value");
return testresults;
}
有什么方法可以让我只使用内置的 ASP.NET 工具来摆脱那些“键”和“值”标签? (即,如果可以避免的话,我宁愿不使用 JSON.NET)
非常感谢! :)
【问题讨论】: