在我的WPF MVVM應用程序中,我從JSON格式的restful WCF服務獲取數據。 我使用JavaScriptSerializer反序列化這些數據。但是,如果有的話,這是無法處理空值。在這種情況下,我必須發送一些值,而不是我不想發送的DBnull值。JavaScriptSerializer序列化程序不處理空值
WCF服務:
[OperationContract]
[WebInvoke
(
Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "ReportEmployee/All"
)
]
dcReportEmployee[] GetReportEmployee();
其中dcReportEmployee是datacontract
[DataContract]
public class dcReportEmployee
{
[DataMember]
public string Name { get; set; }
[DataMember]
public Int32 Status { get; set; }
}
在WPF應用
:
string sUrl = "LocalHostURL";
System.Net.WebRequest request = System.Net.HttpWebRequest.Create(sUrl);
request.Method = "GET";
request.UseDefaultCredentials = true;
request.ContentLength = 0;
System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
Stream objResponseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(objResponseStream);
string objResponseString = reader.ReadToEnd();
response.Close();
JavaScriptSerializer objJsonserialiser = new JavaScriptSerializer();
T[] arrResult = objJsonserialiser.Deserialize<T[]>(objResponseString);
這裏如果objResponseString是含有任何空值,那麼它的失敗。我本可以發送一些空值的替換,但我不希望這樣。
對此有何想法?
可能重複的[可以JavaScriptSerializer排除具有空/默認值的屬性?](http://stackoverflow.com/questions/1387755/can-javascriptserializer-exclude-properties-with-null-default-values) – flup