我将以下JSON发送到接受Stream作为参数的WCF服务。
{"ReservationStream":
{"clientFirstName":"Abe",
"clientLastName":"Lincoln",
"clientPhone":"0544944860",
"clientEmail":"abe@mail.com",
"pickupLocationID":"3699",
"pickupAddressString":"JFK Airport Terminal 1",
"pickupFlightNumber":"LY001",
"pickupAirline":"El Al",
"pickupAirportName":"John F Kennedy Intl",
}
}
在调试器中,stream变量按预期显示所有值。我想反序列化为如下定义的类:
public class ReservationStream
{
public String clientFirstName { get; set; }
public String clientLastName { get; set; }
public String clientPhone { get; set; }
public String clientEmail { get; set; }
public String pickupLocationID { get; set; }
public String pickupAddressString { get; set; }
public String pickupFlightNumber { get; set; }
public String pickupAirline { get; set; }
public String pickupAirportName { get; set; }
}
当我打电话
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
ReservationStream structuredReservations = javaScriptSerializer.Deserialize<ReservationStream>(strJSON);
不会抛出任何异常,但所有字段都具有空值。我的错误在哪里?
答案 0 :(得分:1)
我认为这是因为您的JSON对象嵌套在ReservationStream
标记内。
尝试使用:
{
"clientFirstName":"Abe",
"clientLastName":"Lincoln",
"clientPhone":"0544944860",
"clientEmail":"abe@mail.com",
"pickupLocationID":"3699",
"pickupAddressString":"JFK Airport Terminal 1",
"pickupFlightNumber":"LY001",
"pickupAirline":"El Al",
"pickupAirportName":"John F Kennedy Intl"
}
答案 1 :(得分:0)
您尝试反序列化包含 ReservationStream对象的对象而不是ReservationStream对象本身。
解决这个问题的一种方法是创建一个包含ReservationStream的包装类并使用它反序列化:
public class WrapperClass
{
public ReservationStream ReservationStream { get; set; }
}
WrapperClass wrapperClass = serializer.Deserialize<WrapperClass>(strJSON);
答案 2 :(得分:0)
我曾经遇到同样的问题,我删除了javaScriptSerializer并直接使用了像这样的方法参数
public static string UpdateTemplateProperties(TemplateProperties templateProperties)
{
var x = templateProperties.Something
}
我有
的javascript对象结构确保参数名称与javascript对象文字中的第一个对象名称相同