【发布时间】:2015-08-11 14:54:05
【问题描述】:
我收到的格式是:
[{"item1": "value1","item2": "value2"},{"item1": "value2","item2": "value4"}]
主要问题似乎是无论我尝试什么,我都会收到错误“Type System.Collections.Generic.IDictionary'2 Is Not supported for deserialization of an array.”
搜索互联网只需要我将数组包装在顶级变量中(即数组不能位于根级别)。很遗憾,我无法更改接收数据的内容和方式。
这是我所有的代码,我尝试过的东西都被注释掉了:
我的测试 ajax 用来模拟第三方将发送给我的内容:
“test.html”的头:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<SCRIPT type="text/javascript">
<!--
function testPost() {
var theData;
theData = '[{"item1": "value1","item2": "value2"},{"item1": "value2","item2": "value4"}]';
//theData = '{"item1": "value1","item2": "value2"}'; //I can make a single array element work, but that is not what I will receive
$.ajax({
type: "POST",
contentType: "application/json",
url: "test2.aspx/test_array",
data: theData,
dataType: "json",
success: function(msg) {
alert(msg);
}
});
}
//-->
</SCRIPT>
“test.html”的正文
<input type="submit" value="test Post" onClick="testPost();" /><br>
Test.aspx.vb 全文:
Imports System.Collections.Generic
Imports System.Web.Script.Serialization
Partial Class test2
Inherits System.Web.UI.Page
' Public Shared Function test_array(ByVal item1 As String, ByVal item2 As String) As String ' works if I am passing a single array element
' Public Shared Function test_array(ByVal theobj As Object) As String ' "Type System.Collections.Generic.IDictionary`2 Is Not supported for deserialization of an array."
' Public Shared Function test_array(ByVal theobj As List(Of Object)) As String' "Type System.Collections.Generic.IDictionary`2 Is Not supported for deserialization of an array."
' Public Shared Function test_array(ByVal theobj As Test_Request) As String ' "Type System.Collections.Generic.IDictionary`2 Is Not supported for deserialization of an array."
' Public Shared Function test_array(ByVal theobj As List(Of Test_Request)) As String' "Type System.Collections.Generic.IDictionary`2 Is Not supported for deserialization of an array."
' Public Shared Function test_array(ByVal theobj As Dictionary(Of String, String)) As String' "Type System.Collections.Generic.IDictionary`2 Is Not supported for deserialization of an array."
<System.Web.Services.WebMethod> _
Public Shared Function test_array(ByVal d As List(Of Test_Request)) As String
Return "test"
End Function
End Class
“Test_Request”类:
Public Class Test_Request
Property item1 As String
Get
Return m_item1
End Get
Set(value As String)
m_item1 = value
End Set
End Property
Private m_item1 As String
Property item2 As String
Get
Return m_item2
End Get
Set(value As String)
m_item2 = value
End Set
End Property
Private m_item2 As String
End Class
如果我未能提供任何需要的数据,请告诉我。我已经转了几天了,在决定在这里发帖之前,我试图在这个测试模型中重新创建我所有失败的尝试。希望我只是忽略了一些东西。
【问题讨论】:
-
听起来您的反序列化代码正试图将列表强制转换为字典
-
我正在收集的是 asp.net 如何接收发布到 WebMethod 的数组,但我还没有找到解决它的方法。
-
你应该发布反序列化代码
-
我已经完整地发布了每一行代码(除了一些 HTML 文件)。
标签: asp.net json vb.net asp.net-web-api