反序列化:“数组的反序列化不支持类型”

时间:2019-05-21 10:30:17

标签: javascript asp.net json vb.net deserialization

当我单击删除图层的按钮时,此代码给我一条错误消息:

$scope.removeLayer = function () {
    var data = {'layerArr': convertedLayerArray,}}

我收到一条错误消息:

“数组反序列化不支持类型'WebCE.LayerArrVb'。”

以下是相关的类,Web方法和Ajax调用:

Public Class LayerArrVb
Public Property absoluteEndM As Integer
Public Property absoluteStartM As Integer
Public Property endDate As String
Public Property endMetres As Integer
Public Property layerDate As String
Public Property name As String
Public Property startMetres As Integer
Public Property thickness As Integer
End Class


<WebMethod>
Public Function ChartInfo(layerArr As List(Of LayerArrVb)) As List(Of 
LayerArrVb)
    Return layerArr
End Function

由于存在不兼容的JSON日期格式,因此我必须创建ConvertedLayerArray:

convertedLayerArray = [];
        for (let i = 0; i < layerArr2.length; ++i) {
            var convertedEndDate = new 
            Date(parseInt(layerArr2[i].endDate.substr(6)));
            var convertedEndDate2 = convertedEndDate.getFullYear() + ' ' + 
             (convertedEndDate.getMonth() + 1) + ' ' + 
             convertedEndDate.getDate();

            var convertedLayerDate = new 
             Date(parseInt(layerArr2[i].layerDate.substr(6)));
            var convertedLayerDate2 = convertedLayerDate.getFullYear() + ' 
            ' + (convertedLayerDate.getMonth() + 1) + ' ' + 
             convertedLayerDate.getDate();

            convertedLayerArray.push({
                "absoluteEndM": layerArr2[i].absoluteEndM,
                "absoluteStartM": layerArr2[i].absoluteStartM,
                "endDate": convertedEndDate2,
                "endMetres": layerArr2[i].endMetres,
                "layerDate": convertedLayerDate2,
                "name": layerArr2[i].name,
                "startMetres": layerArr2[i].startMetres,
                "thickness": layerArr2[i].thickness
            });
        }

        $.ajax({
            type: "POST",
            url: "WebService.asmx/ChartInfo",
            async: true,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ "layerArr": convertedLayerArray}),
            success: function (data) {
                result = data;
            },
            error: function (a, b, c) {
                alert("Ajax call to ChartInfo failed");
            }
        });

谢谢

1 个答案:

答案 0 :(得分:1)

您应按如下所示向课程添加<Serializable()>属性

<Serializable()>
Public Class LayerArrVb
Public Property absoluteEndM As Integer
Public Property absoluteStartM As Integer
Public Property endDate As String
Public Property endMetres As Integer
Public Property layerDate As String
Public Property name As String
Public Property startMetres As Integer
Public Property thickness As Integer
End Class

SerializableAttribute属性应用于一种类型,以指示可以序列化此类型的实例。 您可以从此link

中阅读文档