【问题标题】:Can't receive JSON array being posted to me by third party无法接收第三方向我发布的 JSON 数组
【发布时间】: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


【解决方案1】:

在我看来,您正在尝试反序列化一个字典对象数组。我会做如下的事情。

在服务器端:

<HttpPost>
<Route("test")>
Public Function test(data As YourArray) As String

    Return "done"
End Function
-----------

Public Class YourArray
    Inherits List(Of YourDictionaryObject )
End Class

Public Class YourDictionaryObject 
    Inherits Dictionary(Of String, String)
End Class 

我刚刚测试过,我可以确认以下方法确实有效:

function doTest() {
    var theData = [];
    theData.push({
        item1: "value1",
        item2: "value2"
    });
    theData.push({
        item1: "value3",
        item2: "value4"
    });

    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "somewhere/test",
        data: JSON.stringify(theData),
        dataType: "json",
        success: function(msg) {
            alert(msg);
        }
    });


}

我检查了一下,请求有效负载看起来像这样:

[{"item1":"value1","item2":"value2"},{"item1":"value3","item2":"value4"}]

这在服务器端被反序列化为 YourArray。顺便说一句,我使用的是 Asp.net Web Api(不是 WCF,其中序列化/反序列化字典可能很棘手)。

【讨论】:

  • 我相信我缺少 工作的“导入”语句,你能告诉我那是什么(或者它可能是 .net 的版本)。我使用 .net 2.0 将此应用程序作为“网络表单”。
  • 看来您使用的是 Asp.net webforms,而我的解决方案依赖于 asp.net web api。无论如何,我使用的是标准的 web api 路由属性,如下所述:blogs.msdn.com/b/webdev/archive/2013/10/17/…
  • 我建议使用更新版本的 .net。并参考此链接以将 web api 与 webforms 一起使用:asp.net/aspnet/overview/whats-new-in-visual-studio-2013/… 基本上,下载 Visual Studio 2015 社区(它是免费的),你应该很高兴。
  • 非常感谢您提供的链接。如果我没记错的话,由于项目中其他 WebMethods 的一些限制,我使用的是旧版本的 .net。我敢打赌,我可以通过 web api 和属性路由来克服这些问题。我现在会将您的答案标记为已接受,然后阅读、学习、重写所有这些内容。
  • 不客气。我在 WCF 中序列化/反序列化 json 字典时遇到了困难。切换到 asp.net web api 使这类事情变得如此简单。我建议花时间移动 wep api;以后会为您省去很多麻烦,并且使用起来很有趣。祝你好运!
【解决方案2】:

你的 poco 错了。 Item1 和 Item2 需要是字符串数组

Public Class RootObject
       Public Property item1() As String
       Public Property item2() As String
End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多