首页 新闻 会员 周边 捐助

JsonConvert转化对象问题

0
悬赏园豆:10 [已解决问题] 解决于 2020-11-19 15:24

我一个对象里面有个对象

然后这个对象里面有个集合属性
我序列化时调试的时候时候是有答案选项这个值的

但是对应的选项这个值却为null

是什么原因啊

问题补充:

测试的josn:[{"QuestionBaseType":null,"QuestionType":1,"QuestionTitle":"单选题ceshi1","QuestionSelectList":["选项B","选项C","选项D"],"Answer":"A","ErrorAnalysis":"这是一个正确答案","CreateTime":"0001-01-01T00:00:00","Errors":null,"Id":0},{"QuestionBaseType":null,"QuestionType":3,"QuestionTitle":"消防验收属于二级计划ceshi1","QuestionSelectList":["错误"],"Answer":"A","ErrorAnalysis":"这是一个正确答案","CreateTime":"0001-01-01T00:00:00","Errors":null,"Id":0},{"QuestionBaseType":null,"QuestionType":3,"QuestionTitle":"项目在报集团核准时,工期可为相对时间,在项目批复后应同步修正为绝对时间ceshi1","QuestionSelectList":["错误"],"Answer":"A","ErrorAnalysis":"这是一个正确答案","CreateTime":"0001-01-01T00:00:00","Errors":null,"Id":0}]

灬丶的主页 灬丶 | 初学一级 | 园豆:9
提问于:2020-11-19 10:35
< >
分享
最佳答案
0

把代码贴全, 把测试的json也都贴出来.

大概率是我猜是select和selectlist的问题, 可以考虑把list加上属性 忽略json序列化和反序列化.

收获园豆:10
czd890 | 专家六级 |园豆:14488 | 2020-11-19 10:48

什么意思,不是很明白

灬丶 | 园豆:9 (初学一级) | 2020-11-19 10:49

@灬丶: 我还以为json里面的字段是selects, 原来是selectslist, 那你把IList<> 换成 List<string> 或者string[]

czd890 | 园豆:14488 (专家六级) | 2020-11-19 11:02

@czd890: 我换成List<string>还是为null

灬丶 | 园豆:9 (初学一级) | 2020-11-19 11:21

@灬丶: 代码用文本贴出来,贴全

czd890 | 园豆:14488 (专家六级) | 2020-11-19 13:11

@czd890:

public class ExamQuestionApply : DomainBase<int>
{
/// <summary>
/// 题库id
/// </summary>
public string QuestionBank { get; set; }

    /// <summary>
    /// 培训资料id
    /// </summary>
    public int? ExamTrainMaterialId { get; set; }

    /// <summary>
    /// 提交人
    /// </summary>
    public int SubmitUser { get; set; }

    /// <summary>
    /// 题目
    /// </summary>
    public string QuestionsJson { get; set; }
    /// <summary>
    /// 题目
    /// </summary>
    public List<ExamQuestion> ExamQuestions
    {
        get
        {
            if (string.IsNullOrEmpty(QuestionsJson))
                return new List<ExamQuestion>();
            return JsonConvert.DeserializeObject<List<ExamQuestion>>(QuestionsJson);
        }
        set
        {
            QuestionsJson = JsonConvert.SerializeObject(value);
        }
    }

    /// <summary>
    /// 直管领导
    /// </summary>
    public int DeptManager { get; set; }

    /// <summary>
    /// 提交时间
    /// </summary>
    public DateTime SubmitDate { get; set; }

    /// <summary>
    /// 流程结束日期
    /// </summary>
    public DateTime? WorkFlowEndDate { get; set; }

}

[Serializable]
public class ExamQuestion:DomainBase<int>
{
/// <summary>
/// 题库
/// </summary>
public string QuestionBaseType { get; set; }

    /// <summary>
    /// 题型
    /// </summary>
    public QuestionType QuestionType { get; set; }

    /// <summary>
    /// 题目
    /// </summary>
    public string QuestionTitle { get; set; }

    /// <summary>
    /// 选项
    /// </summary>
    private string QuestionSelects { get; set; }
    //private IList<string> _questionSelectList;
    /// <summary>
    /// 选项
    /// </summary>
    public List<string> QuestionSelectList
    {
        get
        {
            if (string.IsNullOrEmpty(QuestionSelects))
                return new List<string>();
            //_questionSelectList = QuestionSelects.Split('|');
            return QuestionSelects.Split('|').ToList();
        }
        set
        {
            //_questionSelectList = value;
            QuestionSelects = string.Join("|", value);
        }
    }
灬丶 | 园豆:9 (初学一级) | 2020-11-19 13:12

@czd890: 没啥好贴的,就是这样的

灬丶 | 园豆:9 (初学一级) | 2020-11-19 13:13

@灬丶:

private string QuestionSelects {get;}=>string.join(",",_questionSelectList);
private List<string> _questionSelectList=new List<string>();
public List<string> QuestionSelectList
    {
        get
        {
          return _questionSelectList;
        }
        set
        {
            _questionSelectList = value;
        }
    }
czd890 | 园豆:14488 (专家六级) | 2020-11-19 14:37

@czd890: 谢了,我自己这里弄好了,这样写的
/// <summary>
/// 选项
/// </summary>
public string QuestionSelects
{
get
{
if (QuestionSelectList == null)
return "";
return string.Join("|", QuestionSelectList);
}
set
{
QuestionSelectList = QuestionSelects.Split('|').ToList();
}
}
//private IList<string> _questionSelectList;
/// <summary>
/// 选项
/// </summary>
public List<string> QuestionSelectList{ get; set; }

灬丶 | 园豆:9 (初学一级) | 2020-11-19 14:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册