Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, October 27, 2009 7:03 PM
Hi!
I'm having trouble deserializing the following object in a .net 3.5 webservice.
The webservice looks like this:
[WebMethod]
public bool UpdateGroupBooking(GroupBooking groupBooking) {
if (GroupBookings.CreateGroupBooking(groupBooking)) {
return true;
} else {
return false;
}
}
The data it receives looks like this:
{"groupBooking":{"bookingNumber":"Karina Randers","navisionBookingNumber":"51035","bookingId":0,"password":"","destinationCode":"VALT","maxParticipants":10,"arrivalDate":"2010-02-12T23:00:00.000Z","departureDate":"2010-02-19T23:00:00.000Z","depositPaymentDate":"2009-10-26T23:00:00.000Z","balancePaymentDate":"2009-12-31T23:00:00.000Z","prices":[{"__type":"DANSKIXtreme.data.GroupPrice","id":0,"transportType":1,"salesPrice":0,"maxParticipants":0}]}}
My problem lies with the "prices" object.
It's of the type PriceGroupCollection which is a List<GroupPrice>
Without the "__type" declaration the deserializer cannot recognize the object, and with the "__type" declaration I get the following error:
Operation is not valid due to the current state of the object.
In my GroupBooking class I have the prices object like this:
public GroupPriceCollection prices { get; set; }
The GroupPrice class looks like this:
[DataContract]
public class GroupPrice {
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[DataMember]
public int id { get; set; }
/// <summary>
/// Gets or sets the sales price.
/// </summary>
/// <value>The sales price.</value>
[DataMember]
public int salesPrice { get; set; }
/// <summary>
/// Gets or sets the type of the transport.
/// </summary>
/// <value>The type of the transport.</value>
[DataMember]
public int transportType { get; set; }
/// <summary>
/// Gets or sets the max participants.
/// </summary>
/// <value>The max participants.</value>
[DataMember]
public int maxParticipants { get; set; }
}
If I don't send along the prices object everything deserializes perfectly
What am I doing wrong?
If you need more info please let me know!
Regards,
Peter
All replies (5)
Thursday, October 29, 2009 2:39 AM ✅Answered
Did you set it as scriptmethod? Please check this link: http://forums.silverlight.net/forums/t/9726.aspx
Thursday, October 29, 2009 3:56 AM ✅Answered
Hi,
U might have to call the Deserialize function recursively, but I'll show you how Deserialize a collection into List<T>.
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<GroupPrice> groupPriceList = null;
groupPriceList = serializer.Deserialize<List<GroupPrice>>(jsonInput);
Thursday, October 29, 2009 6:41 PM ✅Answered
Thanks for your help guys. I managed to get it working by passing the object as a string and then deserialize it using json.net which supports deserialization of nested list objects.
Wednesday, October 28, 2009 12:39 AM
Hi,
You could use System.Web.Script.Serialization.JavaScriptSerializer class. It supports the method Deserialize using which you could easily deserialize JSON. More information here,
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
Hope that helps!
Wednesday, October 28, 2009 5:07 AM
Hi!
Thanks for your reply but I'm not sure this is what I'm looking for.
The class deserializes fine when I'm not including this part:
"prices":[{"__type":"project.data.GroupPrice","id":0,"transportType":1,"salesPrice":0,"maxParticipants":0}]
In my json-string.
I don't call the deserializer specifically - I just tell the webmethod which object is getting passed in like this:
public bool UpdateGroupBooking(GroupBooking groupBooking)
"prices" is of type "GroupPriceCollection" (List<GroupPrice>) which is declared in the GroupBooking class like this:
public GroupPriceCollection prices { get; set; }
Why do I get the error message:
"Operation is not valid due to the current state of the object."
//Peter