我有一個接受Order類的WebAPI。我創建了Order類並將其序列化,並將其轉換爲字符串,然後將其傳遞給我的web api。數據都在那裏,但是當我在API端反序列化它時(僅用於測試,因爲我得到一個空引用錯誤),我的大多數對象都返回爲空(除了對象的頂層以外)API接受一個對象,傳遞一個對象爲json,只取對象的頂層
Order類:
[Serializable]
public class Order
{
public Address Address { get; set; }
public DateTime OrdereDate { get; set; }
public DateTime DeliveryDate { get; set; }
public IList<Product> ProductList { get; set; }
}
我原來的JSON字符串,我調用API的方法是像這樣
{
"Address" : {
"AddressLine1" : "line1",
"AddressLine2" : "",
"City" : "Test",
"Province" : "ON",
"PostalCode" : "90210",
"Country" : "US",
"Email" : "[email protected]",
"Phone" : "234567876"
},
"OrdereDate" : "\/Date(1400198100000)\/",
"DeliveryDate" : "\/Date(1400753100000)\/",
"ProductList" : [{
"ProductCode" : "GT",
"Name" : null,
"Description" : null,
}, {
"ProductCode" : "CI",
"Name" : null,
"Description" : null,
}
}
這就是被獲得通過使用該API,但如果我反序列化對象返回到Order對象中,Address和ProductList都是null。
編輯:其他類被標記爲串行化以及
[Serializable]
public class Product
{
public string ProductCode { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
[Serializable]
public class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
我的序列化/ API調用
WebClient client = new WebClient();
JavaScriptSerializer js = new JavaScriptSerializer();
client.Headers[HttpRequestHeader.ContentType] = "application/json";
string serialisedData = js.Serialize(order);
var response = client.UploadString("mysite.com/Order/NewOrder", serialisedData);
'Product'和'Address'類是否標記爲[[Serializable]]? – EkoostikMartin
您使用的是json串行器/解串器(並且它是兩側相同的)? – Tim
請顯示引用的類,以便此問題可以回答。 –