I am a java developer and I am working on .net first time. I have used JSON with YUI, but this is first time I am using JQUERY. I have converted Java script object to JSON string using JSON.stringify() and I am getting the same JSON string in code behind, but when I tried to deserialize to the .net object, I am getting the value for property which is Integer, but I not getting the value for String object.
// Client Side
$("#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');
});
});
//Code Behind
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
// Value Object
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":""}]"
It looks like the deserializtion isn’t behaving as expected due to two of your field names being different.
If I changed the property names
PRODtoPRODUCEandORDER_NUMBERtoORDERNUMBERI could get it working. Note that you can leave the private field names as they are, it is just the property names that need to change.