Javascript 使用字典将对象传递给WebMethod<;字符串,int>;参数
我有这个错误 无法将类型为\u0027System.String\u0027的对象转换为类型为\u0027System.Collections.Generic.DictionaryJavascript 使用字典将对象传递给WebMethod<;字符串,int>;参数,javascript,jquery,asp.net,json,json.net,Javascript,Jquery,Asp.net,Json,Json.net,我有这个错误 无法将类型为\u0027System.String\u0027的对象转换为类型为\u0027System.Collections.Generic.Dictionary2[System.String,System.Int32]\u0027,“StackTrace”:“at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(对象o,类型类型,JavaScriptSerializer序列
2[System.String,System.Int32]\u0027,“StackTrace”:“at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(对象o,类型类型,JavaScriptSerializer序列化程序,布尔throwOnError,对象和convertedObject)\r\n位于System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(对象o,类型类型,JavaScriptSerializer序列化程序,布尔throwOnError,对象和convertedObject)\r\n位于System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(对象o,类型,JavaScriptSerializer序列化程序序列化程序)\r\n位于System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary
2 rawParams)\r\n位于System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象目标,IDictionary2参数)\r\n位于System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext上下文,WebServiceMethodData方法数据,IDictionary
2 rawParams)\r\n位于System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData方法数据)”,“ExceptionType”:“System.InvalidOperationException”
当我通过Dictionary将数组传递给WebMethod时如果它是webapi的WebMethod,那么您不能将list或Dictionary类型的对象传递给方法,您只需要传递string参数,要传递这些对象,您需要将它们序列化为string,然后可以使用那根绳子 Webmethod消费代码:
Dictionary<string,int> objstring = new Dictionary<string,int>
var serializer = new JavaScriptSerializer();
var serializedstring = serializer.Serialize(objstring);
Dictionary objstring=新字典
var serializer=新的JavaScriptSerializer();
var serializedstring=serializer.Serialize(objstring);
然后将serializedstring传入参数
在反序列化时执行同样的操作,只需从webapi方法中的序列化程序对象调用反序列化方法
我希望这对您有所帮助。您是否试图在WebMethod/webapi项目的参数中传递Dictionary对象??否我在forloop中创建新对象以将其推入数组并生成JSON.stringify(数组),但得到相同的错误我想通过JSON函数[WebMethod]公共字符串保存(Dictionary段落)将Dictionary传递给WebMethod{ }