小编典典

将JavaScript映射传递给json wcf服务

json

我想将关联数组传递给json wcf服务。

因此,在JavaScript中,我与此类似:

var map = { };
map['a'] = 1;
map['b'] = 2;
map['c'] = 3;

在我的wcf服务中,我希望有一个字典:

[OperationContract][WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public void setDictionary(Dictionary<string, int> myDictionary);

但是它将地图作为[Object object]发送而不是序列化,因为’map’实际上只是我为其分配属性的对象。

有谁知道我可以正确地序列化它以使其由WCF服务反序列化为Dictionary对象?


阅读 349

收藏
2020-07-27

共1个答案

小编典典

默认情况下,WCF不表示Dictionary为JSON对象-
而是将它们表示为键/值对的数组。因此,要将该地图发送到WCF服务,您需要对其进行适当的隐藏(请参见下面的代码)。

另一种选择是使用自定义消息格式化程序,该消息格式化程序知道如何根据JSON对象填充字典。有关消息格式化程序的更多信息,请查看此博客文章

这显示了将该对象传递给服务的一种方法:

Service.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="StackOverflow_15001755.Service"
                CodeBehind="StackOverflow_15001755.svc.cs" 
                Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

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调用):

<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>
2020-07-27