我是一名java开发人员,我第一次在.net上工作。我在YUI中使用了JSON,但这是我第一次使用JQUERY。我已经使用JSON.stringify()
将Java脚本对象转换为JSON字符串,并且我在后面的代码中获得相同的JSON字符串,但是当我尝试反序列化为.net对象时,我获取的属性值为Integer,但我没有得到String对象的值。
//客户端
$("#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 String
"[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":""}]"
答案 0 :(得分:0)
由于您的两个字段名称不同,看起来deserializtion的行为不符合预期。
如果我将属性名称PROD
更改为PRODUCE
而将ORDER_NUMBER
更改为ORDERNUMBER
,我可以将其设置为正常工作。请注意,您可以保留私有字段名称,它只是需要更改的属性名称。