【问题标题】:WCF dynamic json to DictionaryWCF 动态 json 到字典
【发布时间】:2014-04-22 08:01:51
【问题描述】:

我使用的是 ASPX 4.5。
客户端发送一个带有动态字段的 JSON 对象(每次可以不同)

function storeDataInSession(formData) {
    var data = {};
    data["formData"] = formData;

    $.ajax({
        url: "MY_URL/StoreFormData",
        type: "post",
        data: JSON.stringify(data),
        contentType: 'application/json',
        dataType: 'json',
        success: function (data, textStatus, xhr) {
            console.log(data);
            console.log("success");
        },
        error: function (xhr, textStatus, errorThrown) {
            console.log("failure");
        }
    });
}

在服务器端,我正在尝试将该 JSON 转换为字典,但出现错误 500。

[OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public String StoreFormData(dynamic formData) 
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        Dictionary<string, string> formValues = jss.Deserialize<Dictionary<string, string>>(formData);


        return "aaaaa";
    }

我做错了什么?

【问题讨论】:

  • 为您的服务添加错误处理,这将有助于找出发生了什么错误:stackoverflow.com/questions/23212705/…
  • 错误:错误:无法从MYURL获取元数据
  • 我注意到您使用的是动态的。 WCF 不允许这样做

标签: c# asp.net json wcf dictionary


【解决方案1】:

由于您想将原始数据接收到您的方法参数中,您必须以某种方式实现您的方法:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "form/data",
    RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public String StoreFormData(Stream fileContents)
{
    using (StreamReader reader = new StreamReader(fileContents))
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();

        //here is your json, received from a client
        string jsonData = reader.ReadToEnd();

        // I think for that you case it's better to use Newtonsoft.Json library. It will allow you to parse more complex data
        //JObject data = JObject.Parse(jsonData); 

        //Dictionary<string, string> formValues = jss.Deserialize<Dictionary<string, string>>(formData);
    }

    return "aaaaa";
}

通过这种方式,您将能够以在客户端形成的方式接收纯 json 数据。然后你可以用你需要/想要的方式解析它们。

编辑 1:

附:不要忘记将 UriTemplate = "form/data" 更改为您需要的任何内容。

编辑 2:

我认为对于您的情况,最好使用 Newtonsoft.Json 库。它将允许您解析更复杂的数据:

JObject data = JObject.Parse(jsonData); 

【讨论】:

    【解决方案2】:

    有两个问题:
    1. 我使用了@Legart 提到的动态。
    2.参数是一个json对象。

    有一个可行的解决方案:

    function storeDataInSession(formData) {
        var data = {};
        data["formData"] = JSON.stringify(formData);
    
        $.ajax({
            url: "MYURL/StoreFormData",
            type: "post",
            data: JSON.stringify(data),
            contentType: 'application/json',
            dataType: 'json',
            success: function (data, textStatus, xhr) {
                console.log(data);
                console.log("success");
            },
            error: function (xhr, textStatus, errorThrown) {
                console.log("failure");
            }
        });
    }
    

    服务器端:

    [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public String StoreFormData(string formData) 
        {        
            JavaScriptSerializer jss = new JavaScriptSerializer();
            Dictionary<string, string> formValues = jss.Deserialize<Dictionary<string, string>>(formData);
    
            string test = "test:  ";
            foreach (KeyValuePair<string, string> item in formValues) 
            {
                test += String.Format(" key: {0}  value: {1} ", item.Key, item.Value);
            }
    
    
    
            return formData;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多