无法接收由第三方发布给我的JSON数组

时间:2015-08-11 14:54:05

标签: asp.net json vb.net asp.net-web-api

我收到的格式是:

$(document).ready(function(){
    var off = $(".navbar-static-top").height() + 80; 
    var off2 = $("footer").height() + 30;
    $(".awrap > div:nth-child(1)").attr('data-offset-top', off.toString(); );
    $(".awrap > div:nth-child(1)").attr('data-offset-bottom', off2.toString(); );           
    $(function() {
        var $affixElement = $('div[data-spy="affix"]');
            $affixElement.width($affixElement.parent().width());
    });
});

主要问题似乎是无论我尝试什么,我都会收到错误" [{"item1": "value1","item2": "value2"},{"item1": "value2","item2": "value4"}] " 搜索互联网只需要将数组包装在顶级变量中(即数组不能位于根级别)。不幸的是,我无法改变接收数据的内容和方式。

以下是我尝试注释的所有代码:

我的测试ajax用于模拟第三方将发送给我的内容:

" test.html":

的负责人
Type System.Collections.Generic.IDictionary'2 Is Not supported for deserialization of an array.

" 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.aspx.vb in the complete:

<input type="submit" value="test Post" onClick="testPost();" /><br>

&#34; Test_Request&#34;类:

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

如果我没有包含任何所需数据,请告诉我。我已经在圈子里进行了几天,在决定发布之前,我试图在这个测试模拟中重新创建我失败的所有尝试。希望有一些我忽略的东西。

2 个答案:

答案 0 :(得分:2)

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

在服务器端:

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


}

我查了一下,Request Payload看起来像这样:

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

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

答案 1 :(得分:0)

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

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