2017-04-11 2 views
0

私はデータを収集し、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"]); 
    } 
} 

あなたが見ることができるように、私はActionResultAnswerを持ってActionResultQuestionが含まれているそれぞれのOutcomeActionResultsのリストが含まれている結果を持っています。このような何か:

アウトカム - >一覧(OutcomeActionResult) - > ActionResultQuestion - > ActionResultAnswer

私は、コードをステップ実行すると、すべてのデータが正しく読み込まれていると、すべてがうまくています。しかし、JSONにオブジェクト構造をシリアル化すると、ActionResultAnswer以外のすべてがシリアル化されます。基本的には、構造の最も深いレベルが切り刻まれます。私はこれがなぜ起こっているのか、それが起こらない方法を教えてくれるものは何も見つかりませんでした。

はおそらくここまでオブジェクトをシリアライズコードを配置するべきである:

var response = outcomes.getOutcomesByClientAndDate(clientId, year, month, day, shiftId, dayOfWeek); 
var json = JsonConvert.SerializeObject(response); 

答えて

1

あなたActionResultQuestionクラスのanswerプロパティはパブリックではありません。したがって、デフォルトではJson.Netによってシリアル化されません。

あなたはプロパティを公開することができますいずれか

...

public ActionResultAnswer answer { get; set; } 

か、あなたはそれがない公共ことが意図した場合、あなたはシリアライザがそれを「見る」ことができるように[JsonProperty]属性でマークすることができます:

[JsonProperty] 
ActionResultAnswer answer { get; set; } 
+0

聖なるものです。どのように私はそれをお見逃しですか?ありがとう。 –

関連する問題