简体   繁体   English

WCF动态json到字典

[英]WCF dynamic json to Dictionary

I'm using ASPX 4.5. 我正在使用ASPX 4.5。
The client sends one JSON object with dynamic fields (can be different each time) 客户端发送一个带有动态字段的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");
        }
    });
}

On the server side I'm trying to convert that JSON to Dictionary, but I'm getting error 500. 在服务器端,我试图将JSON转换为Dictionary,但出现错误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";
    }

What am I doing wrong? 我究竟做错了什么?

As you want to receive raw data into your method param, you have to implement your method in a way: 当您想将原始数据接收到方法参数中时,您必须采用以下方式实现方法:

[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";
}

In such a way you will be able to receive plain json data in the way it was formed on a client side. 这样,您将能够以在客户端形成的原始格式接收原始json数据。 Then you can parse them in a way you need/want. 然后,您可以按照需要/想要的方式解析它们。

EDIT 1: 编辑1:

PS Don't forget to change UriTemplate = "form/data" to whatever you need. PS不要忘记将UriTemplate =“ form / data”更改为所需的内容。

EDIT 2: 编辑2:

I think that for your case it's better to use Newtonsoft.Json library. 我认为对于您的情况,最好使用Newtonsoft.Json库。 It will allow you to parse more complex data: 它将允许您解析更复杂的数据:

JObject data = JObject.Parse(jsonData); 

There were 2 problems: 有两个问题:
1. I used dynamic as @Legart mentioned. 1.我使用@Legart提到的dynamic。
2. The parameter was a json object. 2.参数是一个json对象。

There are a working solution: 有一个可行的解决方案:

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");
        }
    });
}

The server side: 服务器端:

[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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM