默认情况下,WCF 不表示Dictionary
为 JSON 对象,而是将它们表示为键/值对数组。因此,要将地图发送到 WCF 服务,您需要适当地对其进行转换(参见下面的代码)。
另一种选择是使用自定义消息格式化程序,它知道如何根据 JSON 对象填充字典。有关消息格式化程序的更多信息,请查看此博客文章。
这显示了将该对象传递给您的服务的一种方法:
服务.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="StackOverflow_15001755.Service"
CodeBehind="StackOverflow_15001755.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
服务.svc.cs:
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace StackOverflow_15001755
{
[ServiceContract]
public class Service
{
static Dictionary<string, int> dictionary;
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public void setDictionary(Dictionary<string, int> myDictionary)
{
dictionary = myDictionary;
}
[WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Dictionary<string, int> getDictionary()
{
return dictionary;
}
}
}
Test.html(HTML/JS 代码,使用 jQuery 进行 ajax 调用):
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="scripts/jquery-1.7.2.js"></script>
<script type="text/javascript" src="scripts/json2.js"></script>
</head>
<body>
<script type="text/javascript">
function StackOverflow_15001755_Test() {
function dictionaryToKVPArray(obj) {
var data = [];
for (var key in obj) {
data.push({ Key: key, Value: obj[key] });
}
return data;
}
function KVPArrayToDictionary(arr) {
var result = {};
arr.forEach(function (item) {
result[item.Key] = item.Value;
});
return result;
}
var map = {};
map['a'] = 1;
map['b'] = 2;
map['c'] = 3;
var data = dictionaryToKVPArray(map);
var baseUrl = "/StackOverflow_15001755.svc";
$.ajax({
type: 'POST',
url: baseUrl + '/setDictionary',
contentType: 'application/json',
data: JSON.stringify({ myDictionary: data }),
success: function (result) {
$('#result').text('Sent the dictionary');
$.ajax({
type: 'GET',
url: baseUrl + '/getDictionary',
success: function (result) {
var newMap = KVPArrayToDictionary(result);
$('#result2').text(JSON.stringify(newMap));
}
});
}
});
}
</script>
<input type="button" value="StackOverflow 15001755" onclick="StackOverflow_15001755_Test();" /><br />
<div id='result'></div><br />
<div id='result2'></div><br />
</body>
</html>