将JavaScript地图传递给json wcf服务 [英] Pass a javascript map to json wcf service
问题描述
我想传递一个关联数组到一个json wcf服务。
I would like to pass an associative array to a json wcf service.
所以在JavaScript中我有一个类似于这样的东西:
So in JavaScript I have something akin to this:
var map = { };
map['a'] = 1;
map['b'] = 2;
map['c'] = 3;
在我的wcf服务中,我想要一个字典:
And in my wcf service I want to expect a Dictionary:
[OperationContract][WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public void setDictionary(Dictionary<string, int> myDictionary);
但是它将地图作为[Object对象]发送,而不是序列化它,因为map是实际上只是一个我正在分配属性的对象。
But it sends the map as an [Object object] rather than serializing it because the 'map' is actually just an object I'm assigning properties to.
有谁知道如何将其序列化到正确的方式,以便将其反序列化为WCF服务的Dictionary对象? / p>
Does anyone know how I can serialize it to correctly to get it deserialized as a Dictionary object by the WCF service?
推荐答案
默认情况下,WCF不代表 Dictionary
作为JSON对象将它们代替为键/值对的数组。所以要将该地图发送到WCF服务,您需要适当地隐藏它(见下面的代码)。
By default, WCF does not represent Dictionary
as JSON objects - it represents them as arrays of key/value pairs instead. So to send that map to the WCF service, you'll need to covert it appropriately (see code below).
另一种方法是使用自定义消息格式化程序知道如何根据JSON对象填充字典。有关消息格式化程序的更多信息,请查看此博客。
Another alternative is to use a custom message formatter, which knows how to populate dictionaries based on JSON objects. For more information on message formatters, check this blog post.
这显示了将该对象传递给您的服务的一种方式:
This shows one way of passing that object to your service:
Service.svc:
Service.svc:
<%@ ServiceHost Language="C#" Debug="true" Service="StackOverflow_15001755.Service"
CodeBehind="StackOverflow_15001755.svc.cs"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Service.svc.cs:
Service.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调用):
Test.html (HTML/JS code, using jQuery for the ajax call):
<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>
这篇关于将JavaScript地图传递给json wcf服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!