Newtonsoft Json不是序列化类
问题描述:
我有我正在使用的以下类来收集数据,然后返回Json中的结构。Newtonsoft Json不是序列化类
public class Outcome {
public int id { get; set; }
public string outcome { get; set; }
public string actionStep { get; set; }
public List<OutcomeActionResult> actionResults { get; set; }
public void setData(SqlDataReader reader, DateData dateData) {
this.id = Convert.ToInt32(reader["id"]);
this.outcome = Convert.ToString(reader["outcome"]);
this.actionStep = Convert.ToString(reader["action_step"]);
this.actionResults = new Outcomes().getActionResultByOutcomeId(this.id, dateData);
}
}
public class OutcomeActionResult {
public int id { get; set; }
public string actionResult { get; set; }
public ActionResultQuestion question { get; set; }
public void setData(SqlDataReader reader, DateData dateData) {
this.id = Convert.ToInt32(reader["id"]);
this.actionResult = Convert.ToString(reader["action_result"]);
this.question = new Outcomes().getActionResultQuestionByActionResultId(this.id, dateData);
}
}
public class ActionResultQuestion {
public int id { get; set; }
public string question { get; set; }
public bool isMultipleChoice { get; set; }
public List<MultipleChoiceOption> multipleChoiceOptions { get; set; }
ActionResultAnswer answer { get; set; }
public void setData(SqlDataReader reader, DateData dateData) {
this.id = Convert.ToInt32(reader["id"]);
this.question = Convert.ToString(reader["question"]);
this.isMultipleChoice = Convert.ToBoolean(reader["is_multi"]);
this.answer = new Outcomes().getActionResultAnswersByIdAndDate(this.id, dateData.year, dateData.month, dateData.day, dateData.shiftId);
}
}
public class ActionResultAnswer {
public int id { get; set; }
public string notes { get; set; }
public int employeeId { get; set; }
public int selectedAnswer { get; set; }
public string answer { get; set; }
public int year { get; set; }
public int month { get; set; }
public int day { get; set; }
public int shiftId { get; set; }
public void setData(SqlDataReader reader) {
this.id = Convert.ToInt32(reader["id"]);
this.notes = Convert.ToString(reader["notes"]);
this.employeeId = Convert.ToInt32(reader["employee_id"]);
this.selectedAnswer = reader.IsDBNull(reader.GetOrdinal("selected_answer")) ? -1 : Convert.ToInt32(reader["selected_answer"]);
this.answer = Convert.ToString(reader["answer"]);
this.year = Convert.ToInt32(reader["year"]);
this.month = Convert.ToInt32(reader["month"]);
this.shiftId = Convert.ToInt32(reader["shift_id"]);
}
}
正如你所看到的,我有结果包含OutcomeActionResults的列表中的每个都包含其中有一个ActionResultAnswer的ActionResultQuestion。事情是这样的:
成果 - >列表(OutcomeActionResult) - > ActionResultQuestion - > ActionResultAnswer
当我通过代码,所有的数据被正确填充,一切都很好。但是,当我将对象结构序列化为JSON时,它将序列化除ActionResultAnswer之外的所有内容。基本上最深层的结构被切断。我一直无法找到任何能够告诉我为什么会发生这种情况的原因,以及如何避免这种情况发生。
或许应该把那个在这里序列化对象的代码:
var response = outcomes.getOutcomesByClientAndDate(clientId, year, month, day, shiftId, dayOfWeek);
var json = JsonConvert.SerializeObject(response);
答
的answer
财产在你ActionResultQuestion
类是不公开的。因此,默认情况下它不会被Json.Net序列化。
您可以让属性public ...
public ActionResultAnswer answer { get; set; }
或者,如果你打算,这不是公开的,你可以用一个[JsonProperty]
属性标记它允许串行“看到”它:
[JsonProperty]
ActionResultAnswer answer { get; set; }
废话。我怎么错过了?谢谢。 –