我有一个类包含另一个类的对象列表。但是当我向wcf方法发送对象列表时它就是null。 这是我的完整代码
这是我的班级: -
public class BOTHCCharges_DETAILS
{
public String THCTerm { get; set; }
public String FromNumberOfContainer { get; set; }
public String ToNumberOfContainer { get; set; }
public String _20_GP { get; set; }
public String _20_HC { get; set; }
public String _40_GP { get; set; }
public String _40_HC { get; set; }
public String Size { get; set; }
public String Weight { get; set; }
}
[DataContract]
[KnownType(typeof(List<BOTHCCharges_DETAILS>))]
public class List_BOTHCCharges_DETAILS
{
[DataMember]
public List<BOTHCCharges_DETAILS> THC_Details { get; set; }
}
//**** ICharges.cs
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "AddTHCCharges")]
String AddTHCCharges(BOTHCCharges_Master OBJBOTHCCharges_Master, List<List_BOTHCCharges_DETAILS> List_BOTHCCharges_DETAILS);
//*** Charges.svc.cs
public string AddTHCCharges(BOTHCCharges_Master OBJBOTHCCharges_Master, List<List_BOTHCCharges_DETAILS> List_BOTHCCharges_DETAILS)
{
//code goes here.
}
这是我通过jquery ajax发送的json对象
{
"OBJBOTHCCharges_Master":
{
"OriginLocationList":"[{\"CountryCode\":\"CHINA,\",\"LocationCode\":\"CNSHA,\",\"Currency\":\"AED,\"}]",
"DesinationLocationList":"[{\"CountryCode\":\"SIN,\",\"LocationCode\":\"SGSIN,\",\"Currency\":\"AED,\"}]",
"CargoWeight":"20,","CargoType":"Consol,General,","Taxable":true,"TradeType":"E"},
"List_BOTHCCharges_DETAILS":
[
["THC_Details",
{
"THCTerm":"","FromNumberOfContainer":"","ToNumberOfContainer":"","_20_GP":"","_20_HC":"","_40_GP":"","_40_HC":"","Size":"20","Weight":"Consol"
},
{
"THCTerm":"","FromNumberOfContainer":"","ToNumberOfContainer":"","_20_GP":"","_20_HC":"","_40_GP":"","_40_HC":"","Size":"20","Weight":"Consol"
}
],
["THC_Details",
{
"THCTerm":"","FromNumberOfContainer":"","ToNumberOfContainer":"","_20_GP":"","_20_HC":"","_40_GP":"","_40_HC":"","Size":"26","Weight":"Consol"
}
]
]
}
jquery call
var wcfServiceUrl = THC.urlAddTHCCharges;
$.ajax({
cache: false,
url: wcfServiceUrl,
data: data,
type: 'POST',
contentType: 'application/json',
dataType: 'json',
beforeSend: function () {
//fnBlockUI('operation in progress..', 'false', 'true', 'null', false);
},
success: function (data) {
if (data.AddTHCChargesResult == 'SUCCESS') {
//$.showMessageBox({ content: "data has been saved successfully", title: 'My information' });
$.showMessageBox({ content: "Data has been saved successfully!", title: 'Save', OkButtonDoneFunction: function () {
location.href = 'Index'
}, type: 'question'
});
}
else {
$.showMessageBox({ content: data.AddDetentionChargesResult, title: 'Error', type: 'stop' });
}
},
error: function (error) {
$.showMessageBox({ content: error.responseText, title: 'Stop', type: 'stop' });
},
complete: function () {
//$.unblockUI();
}
});
*/
另一个类(类成员)的容器列表出来的类null: click to view for your reference. 我需要序列化我的班级成员还是什么?。我是wcf的新手。
答案 0 :(得分:0)
您的JSON与您的班级不符。
此
public class List_BOTHCCharges_DETAILS
{
[DataMember]
public List<BOTHCCharges_DETAILS> THC_Details { get; set; }
}
意味着这个
{ "THC_Details": [ { "THCTerm":"", … }, … ] }
不是这个
{ [ "THC_Details", { "THCTerm":"", … } ], … }
如果JSON与类定义不匹配,您将获得null
,因为绑定器无法反序列化有效负载。