我傳遞一些JSON從客戶端傳回服務器端。確定JSON可以反序列化到什麼類型的對象?
if (historicalJSONAttributes != null) {
$find(ajaxManagerID).ajaxRequestWithTarget(radDock.get_uniqueID(), $.toJSON(historicalJSONAttributes));
}
或
if (customJSONAttributes!= null) {
$find(ajaxManagerID).ajaxRequestWithTarget(radDock.get_uniqueID(), $.toJSON(customJSONAttributes));
}
在這個時間點RadDock不規整使得存在派生類期望僅一個historicalJSONAttribute或customJSONAttribute。提供給RadDock的數據反映了它所持有的內容。我沒有看到一個理由(還沒有?)圍繞其可能的內容構建父控制。
這給我留下了以下問題,但是,我RadDock類的內部:
public void RaisePostBackEvent(string eventArgument)
{
HandleDialogClose(eventArgument);
}
private void HandleDialogClose(string json)
{
JsonConvert.DeserializeObject<HistoricalLocalSettingsJSON>(json);
}
我不能保證傳遞給HandleDialogClose JSON數據是HistoricalLocalSettingsJSON。我是否應該用一個標誌來預先推送我的eventArgument以指示它是哪種類型的數據?沒有完整的重組是否有更好的選擇?
感謝
我的課表:
[DataContract]
public class HistoricalLocalSettingsJSON
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public HistoricalLocalSettingsJSON() { }
public HistoricalLocalSettingsJSON(string commandName, string dockID, string refreshEnabled, string refreshInterval, string chartType, string timeRestrictionEnabled, string timeStart, string timeEnd, string dataPointsEnabled)
{
Logger.InfoFormat("Command Name: {0}, DockID: {1}, RefreshEnabled: {2}, RefreshInterval: {3}, ChartType: {4}, TimeRestrictionEnabled: {5}, TimeStart: {6}, TimeEnd: {7}, DataPointsEnabled: {8}",
commandName, dockID, refreshEnabled, refreshInterval, chartType, timeRestrictionEnabled, timeStart, timeEnd, dataPointsEnabled);
CommandName = commandName;
DockID = dockID;
RefreshEnabled = bool.Parse(refreshEnabled);
RefreshInterval = int.Parse(refreshInterval);
ChartType = (Charts)Enum.Parse(typeof(Charts), chartType);
TimeRestrictionEnabled = bool.Parse(timeRestrictionEnabled);
TimeStart = timeStart;
TimeEnd = timeEnd;
DataPointsEnabled = !string.IsNullOrEmpty(dataPointsEnabled) ? bool.Parse(dataPointsEnabled) : false;
}
[DataMember(Name = "CommandName")]
public string CommandName { get; set; }
[DataMember(Name = "DockID")]
public string DockID { get; set; }
[DataMember(Name = "RefreshEnabled")]
public bool RefreshEnabled { get; set; }
[DataMember(Name = "RefreshInterval")]
public int RefreshInterval { get; set; }
[DataMember(Name = "ChartType")]
public Charts ChartType { get; set; }
[DataMember(Name = "TimeRestrictionEnabled")]
public bool TimeRestrictionEnabled { get; set; }
[DataMember(Name = "TimeStart")]
public string TimeStart { get; set; }
[DataMember(Name = "TimeEnd")]
public string TimeEnd { get; set; }
[DataMember(Name = "DataPointsEnabled")]
public bool DataPointsEnabled { get; set; }
}
[DataContract]
public class CustomLocalSettingsJSON
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public CustomLocalSettingsJSON() { }
public CustomLocalSettingsJSON(string commandName, string dockID, string refreshEnabled, string refreshInterval, string chartType)
{
Logger.InfoFormat("Command Name: {0}, DockID: {1}, RefreshEnabled: {2}, RefreshInterval: {3}, ChartType: {4}",
commandName, dockID, refreshEnabled, refreshInterval, chartType);
CommandName = commandName;
DockID = dockID;
RefreshEnabled = bool.Parse(refreshEnabled);
RefreshInterval = int.Parse(refreshInterval);
ChartType = (Charts)Enum.Parse(typeof(Charts), chartType);
}
[DataMember(Name = "CommandName")]
public string CommandName { get; set; }
[DataMember(Name = "DockID")]
public string DockID { get; set; }
[DataMember(Name = "RefreshEnabled")]
public bool RefreshEnabled { get; set; }
[DataMember(Name = "RefreshInterval")]
public int RefreshInterval { get; set; }
[DataMember(Name = "ChartType")]
public Charts ChartType { get; set; }
}
因爲它的立場CustomLocalSettingsJSON是HistoricalLocalSettingsJSON的子部分。
現在閱讀有關Populate和SubType屬性。我不是100%確定我想創建CustomLocalSettingsJSON是HistoricalLocalSettingsJSON子節的依賴項。這個說法目前是真實的,但是沒有什麼能阻止老闆後來改變主意,我不願意通過做出這樣的假設而失去一大堆工作。 感謝您的指點。 :) –
請參閱編輯..另一個想法爲通用的方式來處理它。您可以序列化數據兩次,並將其作爲字符串傳遞給一般對象。這可以讓服務器代碼一致地解密,然後使用正確的對象時間進行數據的最終反序列化。 –
謝謝你。這看起來不錯,更直觀。我會用這個。 :) –