C# WCF动态json到字典
我正在使用ASPX4.5C# WCF动态json到字典,c#,asp.net,json,wcf,dictionary,C#,Asp.net,Json,Wcf,Dictionary,我正在使用ASPX4.5 客户端发送一个带有动态字段的JSON对象(每次可以不同) 在服务器端,我试图将JSON转换为字典,但出现错误500 [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
客户端发送一个带有动态字段的JSON对象(每次可以不同) 在服务器端,我试图将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";
}
[运营合同]
[WebInvoke(Method=“POST”,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]
公共字符串StoreFormData(动态formData)
{
JavaScriptSerializer jss=新的JavaScriptSerializer();
字典formValues=jss.Deserialize(formData);
返回“AAAA”;
}
我做错了什么?当您希望将原始数据接收到方法参数中时,您必须以以下方式实现您的方法:
[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";
}
有两个问题:
1.我使用了@Legart提到的dynamic。
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;
}
[运营合同]
[WebInvoke(Method=“POST”,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]
公共字符串StoreFormData(字符串formData)
{
JavaScriptSerializer jss=新的JavaScriptSerializer();
字典formValues=jss.Deserialize(formData);
string test=“test:”;
foreach(formValues中的KeyValuePair项)
{
test+=String.Format(“key:{0}值:{1}”,item.key,item.value);
}
返回表单数据;
}
将错误处理添加到您的服务中它将有助于找出错误:错误:错误:无法从MyUrl获取元数据我注意到您使用了动态。WCF是不允许的
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;
}