0

我是一名 java 开发人员,我第一次在 .net 上工作。我在 YUI 中使用过 JSON,但这是我第一次使用 JQUERY。我已经使用将 Java 脚本对象转换为 JSON 字符串,JSON.stringify()并且在后面的代码中得到了相同的 JSON 字符串,但是当我尝试反序列化为 .net 对象时,我得到的属性值是整数,但我没有得到字符串对象的值。

// 客户端

$("#ButtonSave").click(function () {

    //convert gridview to JSON
    var jsonData = new Array();
    $.map($("table[id*=Gridview1] tr"), function (item, index) {
        if ($(item).find("input[type=text]").length > 0) {
            jsonData[index] = new Object();
            jsonData[index].charge = $(item).find("input[type=text][id*=txtCharge]").val();
            jsonData[index].produce = $(item).find("input[type=text][id*=txtProduce]").val();         
            jsonData[index].weight = $(item).find("input[type=text][id*=txtWeight]").val();
            jsonData[index].feet = $(item).find("input[type=text][id*=txtFeet]").val();
            jsonData[index].orderNumber = $(item).find("input[type=text][id*=txtOrderNumber]").val(); 
            jsonData[index].comments = $(item).find("input[type=text][id*=txtComments]").val();
        }
    });

    var jsonStringData = JSON.stringify(jsonData);

    var jqxhr = $.ajax({
        url: "Correction.aspx",
        type: "POST",
        timeout: 10000,
        data: "jsonData=" + jsonStringData
    })
    .error(function () {
        alert('Error');
    })
    .success(function (data) {
        alert('Success');
    });
});

//代码后面

 If Request.Form("jsonData") IsNot Nothing Then

        Dim cita As New TurnDetailVO
        Dim ser As New JavaScriptSerializer()
        Dim items As List(Of TurnDetailVO) = ser.Deserialize(Of List(Of TurnDetailVO))(Request.Form("jsonData"))

        items.RemoveAt(0)

        For Each cita In items
            Console.WriteLine(cita.CHARGE_ID, cita.PROD_ID)
        Next

 End If

// 值对象

Imports Microsoft.VisualBasic
Imports System.Runtime.Serialization

Public Class TurnDetailVO

Private _CHARGE As String
Private _PROD As String
Private _WEIGHT As Integer
Private _FEET As Integer
Private _ORDER_NUMBER As String    
Private _COMMENTS As String


Public Sub New()

    _CHARGE = " "
    _PROD = " "
    _WEIGHT = 0 
    _FEET = 0
    _ORDER_NUMBER = " "
    _COMMENTS = " "

End Sub



Public Property CHARGE() As String
    Get
        Return _CHARGE
    End Get
    Set(ByVal value As String)
        _CHARGE = value
    End Set
End Property

Public Property PROD() As String
    Get
        Return _PROD
    End Get
    Set(ByVal value As String)
        _PROD = value
    End Set
End Property


Public Property WEIGHT() As Integer
    Get
        Return _WEIGHT
    End Get
    Set(ByVal value As Integer)
        _WEIGHT = value
    End Set
End Property


Public Property FEET() As Integer
    Get
        Return _FEET
    End Get
    Set(ByVal value As Integer)
        _FEET = value
    End Set
End Property

Public Property ORDER_NUMBER() As String
    Get
        Return _ORDER_NUMBER
    End Get
    Set(ByVal value As String)
        _ORDER_NUMBER = value
    End Set
End Property

Public Property COMMENTS() As String
    Get
        Return _COMMENTS
    End Get
    Set(ByVal value As String)
        _COMMENTS = value
    End Set
End Property



Public Function Clone() As TurnDetailVO
    Return DirectCast(Me.MemberwiseClone(), TurnDetailVO)
End Function

End Class

// JSON 字符串

"[null,
{"charge":"T860243","produce":"S877020","weight":"13188","feet":"2898","orderNumber":"AN46270","comments":""},
{"charge":"T860243","produce":"S877021","weight":"13538","feet":"2978","orderNumber":"AN46270","comments":""},
{"charge":"T860243","produce":"S877022","weight":"30118","feet":"6618","orderNumber":"AN46270","comments":""},
{"charge":"T860243","produce":"S877023","weight":"23455","feet":"3345","orderNumber":"AN46270","comments":""}]"
4

1 回答 1

0

由于您的两个字段名称不同,因此反序列化的行为似乎不如预期。

如果我将属性名称更改PRODPRODUCEORDER_NUMBERORDERNUMBER我可以让它工作。请注意,您可以保留私有字段名称,只是需要更改的属性名称。

于 2012-10-10T19:50:35.117 回答